What is PYTHONPATH
The PYTHONPATH is a variable in Python that the interpreter uses to locate modules by searching through the paths specified in sys.path.
sys.path is a list that includes, in order
- The directory of the script (if applicable).
- Paths specified in the environmental variable
PYTHONPATH. - Other paths depending on the Python installation on your system.
So, when you import a module, Python searches through these paths in order to find the module.
For example, on my system
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
To view the value of PYTHONPATH
echo $PYTHONPATH
In Linux, you can add a new path to PYTHONPATH like this
export PYTHONPATH="$PYTHONPATH:/path/to/dir/"