Most of the stuff is set up in Python's site.py which is automatically imported when starting the interpreter (unless you start it with the -S option). Few paths are set up in the interpreter itself during initialization (you can find out which by starting python with -S).

Additionally, some frameworks (like Django I think) modify sys.path upon startup to meet their requirements.

The site module has a pretty good documentation, a commented source code and prints out some information if you run it via python -m site.

Answer from Michael on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ sys_path_init.html
The initialization of the sys.path module search path โ€” Python 3.15.2 documentation
To completely override sys.path create a ._pth file with the same name as the shared library or executable (python._pth or python311._pth). The shared library path is always known on Windows, however it may not be available on other platforms. In the ._pth file specify one line for each path to add to sys.path.
๐ŸŒ
Quora
quora.com โ€บ How-is-the-Python-SYS-path-set
How is the Python SYS path set? - Quora
Answer (1 of 3): sys.path is set in order as : 1. The current directory 2. Directories derived from the PYTHONPATH environment variable if set 3. Standard installation directories for this version of Python (for instance [code ]/usr/lib/python . )[/code] - these tend to be the python runtime...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ sys-path-in-python
sys.path in Python - GeeksforGeeks
August 21, 2024 - The following example shows how this can be done. ... APPENDING PATH- append() is a built-in function of sys module that can be used with path variable to add a specific path for interpreter to search.
๐ŸŒ
Xmodulo
xmodulo.com โ€บ change-syspath-pythonpath-python.html
How to change sys.path or PYTHONPATH in Python
September 23, 2020 - Alternatively, you can add the custom module directory in PYTHONPATH environment variable, which will augment the default module search paths used by the Python interpreter. Add the following line to ~/.bashrc, which will have the effect of changing sys.path in Python permanently.
๐ŸŒ
KK's Blog
fromkk.com โ€บ posts โ€บ sys-dot-path-in-python
Sys.path in Python - KK's Blog (fromkk)
June 4, 2026 - Here is the process how sys.path is set in Python, with some parts omitted. Python Command Line Arguments By default, as initialized upon program startup, a potentially unsafe path is prepended to sys.path:
Find elsewhere
Top answer
1 of 3
171

How python finds its modules

Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).

By default, Python looks for its modules and packages in $PYTHONPATH.

To find out what is included in $PYTHONPATH, run the following code in python (3):

import sys
print(sys.path)

How to add a directory

Occasionally

From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:

import sys
sys.path.insert(0, "/path/to/your/package_or_module")

For example:

if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:

import sys
sys.path.insert(0, "/home/myname/pythonfiles")

And I can simply import the file module_1.py by:

import module_1

When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py

For example:

To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `

from <packagename> import module_2

Adding directories to $PYTHONPATH permanently:

Add the following line to your ~/.profile file.

export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add

Subdirectories

From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:

import packagename.subfolder.module_3

Given the fact that all subfolders in the package include their own __init__.py file.

When a module is in the same directory as the script or application

There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.

Example:

If I have a folder, containing script.py and module.py, I can simply import the module by:

import module
2 of 3
7

The correct way to use relative import is:

from ..Common import foo1

And you also need a __init__.py in all your folders.

๐ŸŒ
Learnings
ankursingh.hashnode.dev โ€บ learnings โ€บ #python โ€บ the hitchhiker's guide to `sys.path`
The Hitchhiker's Guide to `sys.path` - Learnings
May 3, 2022 - export PYTHONPATH=$PYTHONPATH:/custom/path/to/modules ยท The specified path will be added to sys.path after the current working directory, but before the default interpreter-supplied paths. If you want to learn how it works, checkout this Stack Overflow discussion ยท The 2nd approach looks good but often times when you are sharing your code with others or setting it up on some other system, you want to make sure that its self sufficient.
๐ŸŒ
IT trip
en.ittrip.xyz โ€บ python
Comprehensive Guide to Understanding and Efficiently Using sys.path in Python | IT trip
October 22, 2024 - Check if the directory containing the module is included in sys.path. Ensure the module name and directory structure are correct. Verify that the PYTHONPATH environment variable is correctly set.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Programming 53 - Sys.path and Changing Module Paths - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals ๐Ÿ’ฏ FREE Courses (100+ hours) - https://calcur.tech/all-in-ones๐Ÿ Python Course - https:...
Published: September 10, 2020
๐ŸŒ
YouTube
youtube.com โ€บ watch
PYTHON sys.path and PYTHONPATH - YouTube
Where does Python look to find modules and packages? How can you adjust the Python search path so that modules that you create can be located.
Published: July 23, 2021
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Sys.path is included in the current directory by default - Python Help - Discussions on Python.org
March 29, 2023 - Hi, I am trying a custom build of python 3.11 on linux, I see that sys.path has current directory by default, I would like to change this behavior by changing the cpython source, >>> import sys >>> sys.path ['', '/usr/local/lib/python311.zip', '/home/mohammed/python/Python-3.11.0/Lib', ...
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ in python script, how do i set pythonpath?
In Python script, how do I set PYTHONPATH? | Codemia
September 24, 2025 - Use sys.path.insert(0, path) when you need your directory to win. A third issue is using PYTHONPATH as a permanent project dependency. That makes local machines, editors, test runners, and deployment environments drift apart. If imports only work after custom shell setup, the project is usually under-packaged.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 592199 โ€บ languages โ€บ add-directory-sys-path
How to add directory to sys.path?? (Jython/Python forum at Coderanch)
I python documentation I read that, sys.path is " Initialized from the environment variable PYTHONPATH , plus an installation-dependent default. (Like we set classpath in Java)??.
๐ŸŒ
GitHub
bic-berkeley.github.io โ€บ psych-214-fall-2016 โ€บ using_pythonpath.html
Using PYTHONPATH โ€” Functional MRI methods
The PYTHONPATH variable has a value that is a string with a list of directories that Python should add to the sys.path directory list.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-check-the-python-system-path-to-find-necessary-modules-397953
How to check the Python system path to find necessary modules | LabEx
The PYTHONPATH environment variable is a list of directories that Python adds to sys.path when it starts. To set the PYTHONPATH environment variable for your current terminal session, you can use the export command.