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
Answer from Jacob Vlijm on askubuntu.com
🌐
ProgramCreek
programcreek.com › python › example › 8 › sys.path
Python Examples of sys.path
tmp = scratch.src + '.tmp' run((scratch_python, '-m', 'pip.__main__', 'install', 'virtualenv', '--target', tmp)) from os import rename rename(tmp, scratch.src) import sys from os.path import realpath # We want to compare the paths themselves as sometimes sys.path is the same # as scratch.venv, but with a suffix of bin/.. if realpath(sys.prefix) != realpath(scratch.venv): # TODO-TEST: sometimes we would get a stale version of venv-update exec_((scratch.python, dotpy(__file__)) + args) # never returns # TODO-TEST: the original venv-update's directory was on sys.path (when using symlinking) sys.path[0] = scratch.src
Discussions

Why is the first element in Python's sys.path an empty string? - Stack Overflow
I noticed, when I launch the Python REPL and do: import sys print(sys.path) The first element of the list is an empty string. This only happens in the REPL. More on stackoverflow.com
🌐 stackoverflow.com
sys.path[0] Is Set Differently From the Rest of sys.path
Also, we currently wait to actually set sys.path[0] (for the main interpreter) until after the readline/rlcompleter modules are imported in pymain_run_python(). More on github.com
🌐 github.com
9
September 25, 2023
python - Why does sys.path start with an empty string? - Stack Overflow
When I print out sys.path in python it apparently always starts with apostrophes denoting an empty string '', why is that? import sys print(sys.path) # ['', and/some/other/paths] ... As initialized upon program startup, the first item of this list, path[0], is the directory containing the script ... More on stackoverflow.com
🌐 stackoverflow.com
September 5, 2018
python - Is it safe to use sys.path[0] in code when pointing to a file location - Stack Overflow
More to the point, why does the script need to lookup sys.path[0] anyway? What's it trying to find? User files? User executables? Python modules? (Anyway, even just within a Windows shop, users can set/prepend any custom PATHs they want. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
python-list.python.narkive.com › WJ5zZHhk › empty-sys-path-0
Empty sys.path[0]
In the library reference, for the 'path' list in the 'os' module: "The first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter." I interpreted it as the directory with the script. I think you mean the "sys" module above, not the ...
🌐
Tooli
tooli.top › posts › python_sys_path
What is sys.path[0] in python - tooli.top
If you run your python code in the way of running a file: ... Then sys.path[0] is /path/to, which is the path of the folder containing example.py.
🌐
Linux Hint
linuxhint.com › sys-path-append-python
How to Use Sys.path.append() in Python – Linux Hint
If done in such a manner, the Python interpreter should be able to locate the requested module, and therefore retrieve the secret variable. The output of the previous operation is as follows: Let’s take another example, this time on a Linux machine. Let’s suppose that we have one file – main.py. Here, sys.path[0] is “/home/kalyani” because that is where main.py is located.
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.

Find elsewhere
🌐
GitHub
github.com › python › cpython › issues › 109853
sys.path[0] Is Set Differently From the Rest of sys.path · Issue #109853 · python/cpython
September 25, 2023 - Feature or enhancement Currently sys.path[0] is set by pymain_run_python() (in Modules/main.c). This happens after pymain_init(), which initializes the runtime, including the rest of sys.path (via getpath.py and site.py). This makes it h...
Author: python
🌐
Python
bugs.python.org › issue1387483
Issue 1387483: sys.path[0] when executed thru a symbolic link - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/42713
🌐
Note.nkmk.me
note.nkmk.me › home › python
Check and Add the Module Search Path in Python: sys.path | note.nkmk.me
May 12, 2023 - In this example, pprint is used to make it easier to read. ... pwd # /Users/mbp/Documents/my-project/python-snippets/notebook python3 print_sys_path.py # ['/Users/mbp/Documents/my-project/python-snippets/notebook', # '/Users/mbp/Documents/lib', # '/Users/mbp/Documents/my-project/python-snippets/notebook', # '/usr/local/Cellar/python/3.7.0...
🌐
pythontutorials
pythontutorials.net › blog › first-argument-of-sys-path-insert-in-python
Python sys.path.insert: What’s the Difference Between 0 and 1 as the First Argument?
The first element of sys.path is '' (current working directory), which contains math.py. Since we inserted ./my_modules at index 1, Python checks the current directory first, importing its math.py instead of the one in ./my_modules. To summarize, the choice between 0 and 1 hinges on search order:
🌐
How to Use Linux
howtouselinux.com › home › understanding sys.path in python
understanding sys.path in Python - howtouselinux
October 9, 2025 - >>>import sys >>> print(sys.path) ['', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python38.zip', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages'] One common use case for sys.path is to extend it when you’re working on a project that uses a custom library. For example, say you’re working on a project that uses the MyLib library.
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not).
🌐
Stack Overflow
stackoverflow.com › questions › 65945355 › is-it-safe-to-use-sys-path0-in-code-when-pointing-to-a-file-location
python - Is it safe to use sys.path[0] in code when pointing to a file location - Stack Overflow
It is not safe, because, as it says in comments, lots of things can modify the order of sys.path. As a workaround, you can import some file in workspace and then get the directory: Copyimport something_in_workspace workspace = os.path.dirname(something_in_workspace.__file__) Usually it is needed to retrieve data files. Better is to package your project and use importlib.resources. See How to read a (static) file from inside a Python package?
🌐
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.
🌐
Python
bugs.python.org › issue39379
Issue 39379: sys.path[0] is already absolute path - Python tracker
January 18, 2020 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/83560
🌐
Reddit
reddit.com › r/learnpython › sys.path and pythonpath
r/learnpython on Reddit: sys.path and pythonpath
December 15, 2016 -

As per the documents:

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

A program is free to modify this list for its own purposes.

So sys.path is basically the path of the current directory and the paths resulting from the PYTHONPATH environmental variable.

If I open up the python repl, sys.path outputs:

['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/home/mike/.local/lib/python3.4/site-packages', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']

However, if I echo PYTHONPATH on the commandline:

echo $PYTHONPATH

Nothing outputs. If the PYTHONPATH environmental variable isn't set then how does python know which paths to use?

🌐
Chris Yeh
chrisyeh96.github.io › 2017 › 08 › 08 › definitive-guide-python-imports.html
The Definitive Guide to Python import Statements | Chris Yeh
August 8, 2017 - If the Python interpreter is run interactively, sys.path[0] is the empty string ''. This tells Python to search the current working directory from which you launched the interpreter, i.e., the output of pwd on Unix systems.
🌐
Python 101
python101.pythonlibrary.org › chapter20_sys.html
Chapter 20 - The sys Module — Python 101 1.0 documentation
In the screenshot above, you can see that the exit script we wrote returned a zero, so it ran successfully. You have also learned how to call another Python script from within Python! The sys module’s path value is a list of strings that specifies the search path for modules.