sys.path[0] is an entry created by the Python executable to refer to the directory of the script being run. If no script is being run, e.g. the REPL has been invoked directly, an empty entry representing the current directory is added.

Answer from Ignacio Vazquez-Abrams on Stack Overflow
🌐
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
Discussions

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
sys.path[0] breaks out of runfile tree.
🐞 bug report Affected Rule py_binary, py_test. Is this a regression? No Description When Python initializes it adds the directory of the script to the sys.path: As initialized upon program startup, the first item of this list, path[0], i... More on github.com
🌐 github.com
7
November 19, 2020
python - Why does sys.path start with an empty string? - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
September 5, 2018
sys.path and pythonpath
There's site-specific (i.e. python environment/install specific) things that happen as well: https://docs.python.org/2/library/site.html#module-site This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s -S option. Importing this module will append site-specific paths to the module search path and add a few builtins. This interacts with the values in sys.prefixand sys.exec_prefixthat were set at compile-time for where to look for site-specific modules. There's also some stuff about enabling user-specific modules so that individual users on multi-user systems can have different sets of modules available to them. More on reddit.com
🌐 r/learnpython
3
5
December 15, 2016
🌐
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
🌐
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
There is some python code at my company that uses sys.path[0] in some file paths. Generally, everybody's sys.path[0] points to c:\\<user.name>\\workspace and I believe mine used to as well.
🌐
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 ...
🌐
GitHub
github.com › bazelbuild › rules_python › issues › 382
sys.path[0] breaks out of runfile tree. · Issue #382 · bazel-contrib/rules_python
November 19, 2020 - 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.
Author: bazel-contrib
🌐
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
Find elsewhere
🌐
Python
bugs.python.org › issue33011
Issue 33011: Embedded 3.6.4 distribution does not add script parent as sys.path[0] - Python tracker
March 6, 2018 - 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/77192
🌐
Python
bugs.python.org › issue16514
Issue 16514: Cryptic traceback when sys.path[0] is None - 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/60718
🌐
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?

🌐
YouTube
youtube.com › watch
PYTHON : Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules - YouTube
PYTHON : Effect of using sys.path.insert(0, path) and sys.path(append) when loading modules [ Gift : Animated Search Engine : https://www.hows.tech/p/recomme...
Published: December 8, 2021
🌐
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).
🌐
Python
bugs.python.org › issue16202
Issue 16202: sys.path[0] security issues - 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/60406
🌐
Esri Community
community.esri.com › t5 › python-questions › arcpy-env-workspace-sys-path-0-gives-different › td-p › 469934
arcpy.env.workspace = sys.path[0] gives different results in PythonWin - BUG?
December 11, 2021 - What you could do instead is test for an empty returned string on sys.argv[0], and instead write your results to a backup or common retrievable location. ... I just thought I would add here that this is not a bug. Using the sys.path will return the PYTHONPATH, which as Wayne mentioned lists the paths that Python will search for when importing modules.
🌐
Michael Currin
michaelcurrin.github.io › dev-cheatsheets › cheatsheets › python › libraries › built-in › sys.html
sys | Dev Cheatsheets
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.
🌐
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 › issue33053
Issue 33053: Avoid adding an empty directory to sys.path when running a module with `-m` - Python tracker
March 12, 2018 - 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/77234
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.

🌐
Python
docs.python.org › 2 › library › sys.html
28.1. sys — System-specific parameters and functions — Python 2.7.18 documentation
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 ...