How to Modify PYTHONPATH on Linux

When you are installing modules in Python that need to be available to Python scripts stored in various locations on the server, you can use PYTHONPATH to specify where those resources are on the server. This environment variable tells Python where to find those modules/libraries.

This post explains how to set environment variables in Linux. You should follow the instructions in that post with the following small modification.

Open up ~/.bash_profile or /etc/profile in a text editor like vi.

Search for PYTHONPATH within the file. You may see something like this:

PYTHONPATH=/home/Steve/CoolPythonModule

This line creates a variable called PYTHONPATH with the value specified.

And somewhere else in the file you may see something like this:

export PATH USER LOGNAME ... PYTHONPATH

This saves the specified variables.

If you don’t have these lines in the file, you should just add them and use the path where the Python module is stored. If you have these lines, you can modify the PYTHONPATH to look in multiple places using syntax such as the following:

PYTHONPATH=/home/Steve/CoolPythonModule:/home/Steve/OtherCoolPythonModule

3 Responses to “How to Modify PYTHONPATH on Linux”

  1. Thank you very for this.

    If I wanted any subdirectories that will be later created under those included directories, what syntax would I have to use?

  2. You know, I’m not sure. One way would be to specify each sub directory separately. But that could be a pain if you have a lot of them. There’s probably some better way to do it. Please let me know if you find out.

  3. See the python documentation:

    http://docs.python.org/install/index.html#inst-search-path

    The use of .pth files in the directory listing the sub directories would be sufficient.

Leave a Reply