sys.path in Python - GeeksforGeeks

sys.path in Python

Last Updated : 13 Nov, 2026

sys.path is a built-in variable in the sys module. It stores a list of directories that Python searches when importing modules. When a module is imported, Python checks whether it is already loaded. If not, the import system looks for it using the available import finders.

Initializing

There are three common ways to configure Python's module search path.

1. Default Initialization

Python automatically initializes sys.path when the interpreter starts.

  • When running a script, Python usually adds the script's directory.
  • In interactive mode, Python generally adds the current working directory.
  • It also includes standard library and site-packages directories.
  • PYTHONPATH environment variable can add more directories.
Python
import sys

print(sys.path)

Output
['/home/guest/sandbox/8761d8b7-4e0b-4729-886d-f2babcd2fea6', '/usr/local/lib/python313.zip', '/usr/local/lib/python3.13', '/usr/local/lib/python3.13/lib-dynload', '/usr/local/lib/python3.13/site-packages']

2. Through Environment Variables

PYTHONPATH environment variable adds directories to Python's module search path. It must be configured before starting the Python interpreter.\

Windows (Command Prompt):

set PYTHONPATH=C:\Users\Vanshi\Desktop

For Linux and macOS:

export PYTHONPATH="/home/user/modules"

Now exectue the following code:

Python
import sys

print(sys.path)

Output
['/home/guest/sandbox/fe31912e-c920-4223-9e7c-63aeda5e78cf', '/usr/local/lib/python313.zip', '/usr/local/lib/python3.13', '/usr/local/lib/python3.13/lib-dynload', '/usr/local/lib/python3.13/site-packages']

3. Appending Path Using append()

append() is a built-in list method. It adds a new directory to the end of sys.path.

Python
import sys

extra_path = 'C:/Users/Vanshi/Desktop'

sys.path.append(extra_path)

print(sys.path)

Output
['/home/guest/sandbox/6b69a949-a577-4cf2-ab52-4aed8f1c66e4', '/usr/local/lib/python313.zip', '/usr/local/lib/python3.13', '/usr/local/lib/python3.13/lib-dynload', '/usr/local/lib/python3.13/site-packages', 'C:/Users/Vanshi/Desktop']

4. Prepending Path Using insert()

insert() is a built-in list method. It adds an element at a specified position. Use insert(0, extra_path) to add a directory at the beginning of sys.path.

Python
import sys

extra_path = 'C:/Users/Vanshi/Desktop'

sys.path.insert(0, extra_path)

print(sys.path)

Output
['C:/Users/Vanshi/Desktop', '/home/guest/sandbox/7797d62a-f86d-4dfc-985e-40db09dd3501', '/usr/local/lib/python313.zip', '/usr/local/lib/python3.13', '/usr/local/lib/python3.13/lib-dynload', '/usr/local/lib/python3.13/site-packages']

Explanation:

  • insert(0, extra_path) places the directory at the beginning.
  • Python searches this directory before directories appearing later in sys.path.
  • It gives modules in the added directory higher import priority.
Comment