How to change sys.path or PYTHONPATH in Python

Question: When I run a Python application, it is not able to find one of imported modules and fails. Looks like the directory of the Python module is not included in the default sys.path used by the Python interpreter. How can I change the default sys.path in Python?

When the Python interpreter executes a program which imports a module, it examines all directory paths listed in sys.path until it finds the module. By default, sys.path is constructed as a concatenation of (1) the current working directory, (2) content of PYTHONPATH environment variable, and (3) a set of default paths supplied by the installed Python interpreter.

If the module you are trying to import is not found in any of the directories defined in sys.path, you will encounter "Import Error: No module named XXXXX" error. The error can occur either because the module is indeed missing on your system, or because the sys.path does not point to the directory where the module is installed. In the latter case, you need to let the Python interpreter know where the module is found. Here is how to change sys.path of your Python interpreter.

Method One

The first method is explicitly change sys.path in your Python program.

You can check the content of the current sys.path as follows.

import sys
print(sys.path)

Once you find that sys.path does not contain the necessary module directory (e.g., /custom/path/to/modules), you can incorporate the directory by adding the following lines before the import statement.

import sys
sys.path.append('/custom/path/to/modules')
. . .
import <your-python-module>

Method Two

Alternatively, you can add the custom module directory in PYTHONPATH environment variable, which will augment the default module search paths used by the Python interpreter.

Add the following line to ~/.bashrc, which will have the effect of changing sys.path in Python permanently.

export PYTHONPATH=$PYTHONPATH:/custom/path/to/modules

The specified path will be added to sys.path after the current working directory, but before the default interpreter-supplied paths.

If you want to change PYTHONPATH variable temporarily, you can use the following command instead.

$ PYTHONPATH=$PYTHONPATH:/custom/path/to/modules python <your-program>

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

One thought on “How to change sys.path or PYTHONPATH in Python

  1. Sometimes python scripts are run from unknown location. Ability to instruct python where to look for modules is blessing.

Leave a comment

Your email address will not be published. Required fields are marked *