Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Python to use additional locations for site-packages

Is there a way to tell Python about additional site-packages locations without modifying existing scripts?

On my CentOS 5.5 server I have a Python 2.7 installation that is installed in /opt/python2.7.2 and there is a site-packages folder at /opt/python2.7.2/lib/python2.7/site-packages.

The reason for this is that I didn't want to disturb the existing Python 2.4 install that shipped with the 5.5 distribution.

However a third party Python application also added a site-packages folder at: /usr/local/lib/python2.7/site-packages and installed itself at that location.

This is partly my fault because I didn't tweak the PREFIX in the application's Makefile before installing, however there's not much I can do about it now.

I know that I can do this:

import sys; sys.path.insert(0, "/usr/local/lib/python2.7/site-packages") 

However it would involve me tracking down every script and adding the above which is not ideal should there be updates in the future.

To get around this I created a symbolic link in /opt/python2.7.2/lib/python2.7/site-packages to the location of this third party application:

 ln -sf /usr/local/lib/python2.7/site-packages/theapp /opt/python2.7.2/lib/python2.7/site-packages/theapp 

This appears to work fine but I'm wondering if there's a better way?

like image 984
Kev Avatar asked Oct 26 '11 10:10

Kev


1 Answers

You can use the Site-specific configuration hook.

"A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path."

In your case, you should be able to achieve what you want by simply dropping in a .pth file containing the path of the directory to include:

[root@home]$ echo "/usr/local/lib/python2.7/site-packages/" > /opt/python2.7.2/lib/python2.7/site-packages/usrlocal.pth 
like image 142
Shawn Chin Avatar answered Oct 25 '22 16:10

Shawn Chin