How to globally modify the default PYTHONPATH (sys.path)?

The site module documentation and Modifying Python's Search Path seem to be what you're looking for.

As far as I understand it, those entries are being added to sys.path by:

  • /usr/lib/python2.6/site.py
  • /usr/lib/python2.6/dist-packages/site.py
    (Change 2.6 to your version of Python.)

The easiest way to change it is to add a file /usr/local/lib/python2.6/dist-packages/site-packages.pth containing ../site-packages.

Alternatively, maybe you can teach the package to use site.getsitepackages()?


You might create a new file called /etc/profile.d/local_python.sh with the contents

PYTHONPATH="/usr/local/lib/python2.6/site-packages/":"${PYTHONPATH}"
export PYTHONPATH

Which will set the PYTHONPATH variable for all logged in users on your system.


I'd like to summarize my findings about python's path modification. There are two ways to do it.

  • .pth file
  • PYTHONPATH

Any .pth file which is found on the default path (see bellow) will get its content included into sys.path. Format of said .pth file is simple: one (folder) path per line. Surprisingly, the paths can be absolute or relative to the .pth file.

Default path is where the interpreter resides and <some-prefix>/lib/python<version>/site-packages where <some-prefix> is usually /usr/.

PYTHONPATH is environmental variable of your operating system. On unix systems you list them by env. Global modification of such variables is done through .sh scripts inside /etc/profile.d/ folder as mentioned by @TestUser16418.