Python: sys.path

By: Dusty Arlia
Published on Tuesday, April 15, 2014, 07:40 PM
Last Updated on Tuesday, July 14, 2015 at 3:55 PM
Total Updates: 2

sys.path is a list of strings that specifies a search path for modules. It is initialized at program startup from the environment variable PYTHONPATH, plus an installation-dependent default. Programs are free to modify this list for its own purposes.

The first item of the 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. The script directory is inserted before the entries inserted as a result of PYTHONPATH.