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

33

21

On a Ubuntu (10.10) system, I have a Python package that installs itself into /usr/local/lib/python2.6/site-packages/. This isn't contained in the default path (sys.path). How do I add this directory to the path?

Setting the $PYTHONPATH environment variable is a solution, of course, but I'm looking for a more elegant way to do this. For example easy_install also puts installed packages in it, my sys.path looks something like this:

['', '/usr/local/lib/python2.6/dist-packages/keyring-0.5.1-py2.6.egg', 
'/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk',  
'/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', 
'/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', 
'/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6', 
'/usr/lib/pymodules/python2.6/gtk-2.0']

so the path is obviously not the default built into the Python binary.

Is there a single config file that contains the entries above? Or in what ways is it possible to modify it?

Latanius

Posted 2011-02-18T20:23:38.183

Reputation: 554

Answers

27

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()?

Mikel

Posted 2011-02-18T20:23:38.183

Reputation: 7 890

1thanks, works perfectly :) (I ended up adding a .pth file to /usr/local/python2.6/dist-packages, containing "../site-packages") – Latanius – 2011-02-18T21:40:37.633

1You can also create a module called sitecustomize.py which site.py tries to import and modify sys.path there. – TestUser16418 – 2011-02-18T22:11:24.130

5

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.

TestUser16418

Posted 2011-02-18T20:23:38.183

Reputation: 810

7This really isn't universal. The /etc/profile.d mechanism will only work for login shells for people with shells that use /etc/profile.d (bash/ksh/zsh). I'm sure csh users won't see this change. Also, will be ignored in cron/at jobs. – Rich Homolka – 2011-02-18T21:43:27.057

2this is a nice way to set global environment variables (yet another thing I learned today), but as I mentioned in the post, I was looking for a more Pythonic way :) – Latanius – 2011-02-18T21:45:30.110

4

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.

katomaso

Posted 2011-02-18T20:23:38.183

Reputation: 41

2It is PYTHONPATH without "_" – heroxbd – 2017-07-09T04:57:52.273

1

For example, if you want to import the suds module which is available as an .egg file:

egg_path = '/home/shahid/suds_2.4.egg'

sys.path.append(egg_path)

import suds
# ... rest of code

Mohammad Shahid Siddiqui

Posted 2011-02-18T20:23:38.183

Reputation: 271