- Using PYTHONPATH¶
- Setting PYTHONPATH more permanently¶
- If you are on a Mac¶
- If you are on Linux¶
- If you are on Windows¶
- Checking system environment variables in Python¶
- How to Find Path Information in Python
- Path information sources
- How to find path information
- Another way to find paths
- About This Article
- This article is from the book:
Using PYTHONPATH¶
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.
The main use of PYTHONPATH is when we are developing some code that we want to be able to import from Python, but that we have not yet made into an installable Python package (see: making a Python package ).
Returning to the example module and script in Where does Python look for modules? :
def func(): print("Running useful function")
import a_module a_module.func()
At the moment, on my machine, PYTHONPATH is empty:
Before we set PYTHONPATH correctly, a_script.py will fail with:
$ python3 scripts/a_script.py Traceback (most recent call last): File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'
Now I set the PYTHONPATH environment variable value to be the path to the code directory:
$ # Set PYTHONPATH to path to the working directory + /code $ # This is for the "bash" shell on Unix / git bash on Windows $ export PYTHONPATH="$PWD/code" $ # Now the script can find "a_module" $ python3 scripts/a_script.py Running useful function
Setting PYTHONPATH more permanently¶
You probably don’t want to have to set PYTHONPATH every time you start up a terminal and run a Python script.
Luckily, we can make the PYTHONPATH value be set for any terminal session, by setting the environment variable default.
For example, let’s say I wanted add the directory /Users/my_user/code to the PYTHONPATH:
If you are on a Mac¶
- Open Terminal.app ;
- Open the file ~/.bash_profile in your text editor – e.g. atom ~/.bash_profile ;
- Add the following line to the end:
export PYTHONPATH="/Users/my_user/code"
If you are on Linux¶
- Open your favorite terminal program;
- Open the file ~/.bashrc in your text editor – e.g. atom ~/.bashrc ;
- Add the following line to the end:
export PYTHONPATH=/home/my_user/code
If you are on Windows¶
Got to the Windows menu, right-click on “Computer” and select “Properties”:
From the computer properties dialog, select “Advanced system settings” on the left:
From the advanced system settings dialog, choose the “Environment variables” button:
In the Environment variables dialog, click the “New” button in the top half of the dialog, to make a new user variable:
Give the variable name as PYTHONPATH and the value is the path to the code directory. Choose OK and OK again to save this variable.
Now open a cmd Window (Windows key, then type cmd and press Return). Type:
to confirm the environment variable is correctly set.
If you want your IPython sessions to see this new PYTHONPATH variable, you’ll have to restart your terminal and restart IPython so that it picks up PYTHONPATH from the environment settings.
Checking system environment variables in Python¶
You can check the current setting of environment variables, using the os.environ dictionary. It contains all the defined environment variables of the shell that started Python. For example, you can check the value of the PYTHONPATH environment variable, if it is defined:
>>> import os >>> os.environ['PYTHONPATH'] '/home/my_user/code'
How to Find Path Information in Python
In the Python programming language, to use code in a package, Python must be able to locate the package and load it into memory. The location information is stored as paths within Python. Whenever you request that Python import a package, Python looks at all the files in its list of paths to find it. The path information comes from three sources.
Path information sources
- Environment variables: Python environment variables, such as PYTHONPATH , tell Python where to find modules on disk.
How to find path information
- Open the Python Shell. You see the Python Shell window appear.
- Type import sys and press Enter.
- Type for p in sys.path: print(p) in a new cell and click Run Cell You see a listing of the path information, as shown in the figure below. Your listing may be different from the one shown in the figure, depending on your platform, the version of Python you have installed, and the Python features you have installed.
Another way to find paths
- In a new cell, type import os and press Enter.
- Type os.environ[‘PYTHONPATH’].split(os.pathsep) and press Enter. When you have a PYTHONPATH environment variable defined, you see a list of paths, as shown in the figure below. However, if you don’t have the environment variable defined, you see an error message instead.
When you list the sys.path contents again, you see that the new entry is added to the end of the list. Likewise, when you want to remove an entry you type sys.path.remove(os.getcwd()) and press Enter. The addition is present only during the current session.