How to set Python environment variable PYTHONPATH on Linux?


To set the PYTHONPATH environment variable on Linux, follow these steps −

Open a terminal window on your Linux system.

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 −

$echo $PYTHONPATH		

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.

Open a terminal window

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 −

$echo $PYTHONPATH

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.

Updated on: 02-May-2023

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements