-   Slackware (
-   -   Is there a good workaround to add search path in python? (

lolnameless 12-25-2012 07:48 AM

Is there a good workaround to add search path in python?
Hi Slackers,

I have non-official software package in prefix=/usr/local instead of /usr.

In Slackware64-current(or maybe all recent Slackware),the official python package has prefix=/usr,which has sys.path like this.

$ python
Python 2.7.3 (default, Jul  3 2012, 19:58:39)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib64/', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr
/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-
packages/PIL', '/usr/lib64/python2.7/site-packages/gtk-2.0']

You see,python libraries installed under /usr/local(or anything else) cant be searched by the /usr/bin/python interpreter. That is the problem.

Another problem is that there is sooo many way to do this,
like,changing PYTHONPATH,install yet another python in /usr/local,
or there are python specific ways like pip and virtualenv.
However,I don't use python(at the moment),i just want to change the search path such that the settings is persistent across update/upgrade/adding new software...etc.I would like to see how you guys do it.


audriusk 12-25-2012 10:11 AM

Open /usr/lib64/python2.7/ and find the following lines:

# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]

Change the second line to:

PREFIXES = [sys.prefix, sys.exec_prefix, '/usr/local']
Or create a file /usr/lib64/python2.7/site-packages/ and put this into it:

import site

Similarly add more paths if there's a need.

lolnameless 12-25-2012 11:26 AM

Thank you Audriusk.
Yes,this is the way to do it.
Mark it as solved~

All times are GMT -5. The time now is 08:57 AM.