PYTHONPATH environment variable
In the python-files directory, I have a few projects cloned from git-hub (flask, curveship and py-vgdl). Whenever I try to start up any of the examples in these projects, I get errors similar to the following:
$ python ~/python-files/py-vgdl/examples/gridphysics/frogs.py Traceback (most recent call last): File "/home/dev/python-files/py-vgdl/examples/gridphysics/frogs.py", line 67, in from vgdl.core import VGDLParser ImportError: No module named vgdl.core
It seems to me that I shouldn’t get this error because I have that PYTHONPATH environmental variable set up? Running the python interactive interpreter:
>>> import os >>> os.environ["PYTHONPATH"] '/home/dev/python-files'
4 Answers 4
Try appending to PYTHONPATH instead of overwriting it completely.
export PYTHONPATH=$PYTHONPATH:/home/dev/python-files
Augment the default search path for module files. [. ]
The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH .
meaning that some values exist in PYTHONPATH and the default search path is also only appended.
Additionally, this blog post (Archive.org link) also explains clearly why you need to append to PYTHONPATH and not overwrite it. Scrolling down to the section — Special cases and examining the search path explains it clearly (unfortunately no relative URL to that link so you’ll have to scroll). Although the user gives the examples on a mac they are very much relevant to any platform
How to set Python environment variable PYTHONPATH on Linux?
Determine the path to your Python module or package. For example, suppose you have a Python module named mymodule located in the /home/user/myproject folder.
Set the PYTHONPATH environment variable to the path of your module or package using the following command −
$export PYTHONPATH=/home/user/myproject:$
This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set.
Note that the path should be separated by a colon (:) on Linux.
Verify that the PYTHONPATH environment variable has been set correctly using the following command −
This should display the path you set earlier, along with any previous paths that were included in PYTHONPATH.
Let us consider few more examples of setting the PYTHONPATH environment variable on Linux −
Set PYTHONPATH to a single path −
$export PYTHONPATH=/path/to/your/python/module
Set PYTHONPATH to multiple paths −
$export PYTHONPATH=/path/to/your/first/python/module:/path/to/your/second/python/module
Set PYTHONPATH to include the current directory −
$export PYTHONPATH=.:$PYTHONPATH
Set PYTHONPATH to include the current directory and a subdirectory −
$export PYTHONPATH=. /subdir:$PYTHONPATH
This sets the PYTHONPATH environment variable to include the current directory (.) and a subdirectory named subdir located in the current directory.
Note that the PYTHONPATH environment variable only affects the current shell session. If you want to set it permanently, you will need to add the export command to a startup script such as .bashrc or .bash_profile.
Determine the location of the folder containing the Python module or package that you want to add to the PYTHONPATH environment variable. For example, let’s say you have a folder called my_module located in your home directory (~/my_module).
Export the PYTHONPATH environment variable to include the folder containing the module or package, using the export command. For example, to add the ~/my_module folder to the PYTHONPATH environment variable, you can run the following command −
$export PYTHONPATH=$PYTHONPATH:~/my_module
The $PYTHONPATH variable is used to append the new folder to the existing value of PYTHONPATH, so that any previously set paths are not overwritten. The colon (:) is used to separate the new path from the existing paths.
It must be noted that this command will only set the PYTHONPATH environment variable for the current terminal session. To make this setting permanent, you will need to add it to your shell’s configuration file (e.g., ~/.bashrc for Bash).
Verify that the PYTHONPATH environment variable has been set correctly. You can do this by running the following command −
This should display the current value of the PYTHONPATH environment variable, including the folder you just added.
Adding multiple folders to PYTHONPATH
$export PYTHONPATH=$PYTHONPATH:~/my_module:~/my_other_module
This will add both the ~/my_module and ~/my_other_module folders to the PYTHONPATH environment variable.
Adding a folder with a space in its path name −
$export PYTHONPATH=$PYTHONPATH:"/path/with/space/my_module"
Note the use of double quotes to enclose the path name containing spaces.
Adding a folder relative to the current directory −
$export PYTHONPATH=$PYTHONPATH:./my_module
This will add the my_module folder located in the current directory to the PYTHONPATH environment variable.
Setting PYTHONPATH to a specific folder only −
$export PYTHONPATH=/path/to/my_module
This will set the PYTHONPATH environment variable to only contain the my_module folder located at /path/to/.
By setting the PYTHONPATH environment variable, you can ensure that Python can find and import the modules and packages you need for your projects, even if they are located outside of the default search paths.
Adding a package that requires a specific version of Python −
$export PYTHONPATH=$PYTHONPATH:/path/to/my_package
If you have multiple versions of Python installed on your system and you want to use a package that requires a specific version, you can add the package’s location to the PYTHONPATH environment variable. This will allow you to use the package with the specific version of Python required.
For example, if you have Python 3.6 installed and a package that requires Python 3.7, you can add the package’s location to the PYTHONPATH environment variable for Python 3.6. Then, when you run Python 3.6, it will be able to find and import the package.
It must be noted that this approach is not a substitute for installing packages with the correct version of Python using a package manager like pip. Instead, it is a workaround for situations where you need to use a package that is not available for the version of Python you have installed.
These examples demonstrate the flexibility of the PYTHONPATH environment variable and how it can be used to customize the search path for Python modules and packages. By setting PYTHONPATH correctly, you can avoid errors related to module imports and ensure that your Python scripts and applications can access the modules and packages they need.
How to add a Python module to syspath?
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):
How to add a directory
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")
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:
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
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: `
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.
If I have a folder, containing script.py and module.py , I can simply import the module by: