Multiple python installations mix their sys.prefix

Tech Support Box tchsprt.box at gmail.com
Mon Jan 24 06:28:19 EST 2011


Hi there
I have several versions of python2.4 installed:
- the OS, rpm-installed one in /usr
- Several other versions in /usr/local, installed with --prefix /usr/
local/inst-shared/<version> --exec-prefix /usr/local/inst/<version>

My problem is when starting one of the versions from /usr/local,
sys.prefix is set as "/usr" instead of the compile-time setting "/usr/
local/inst-shared/<version>" and as a result sys.path contains paths
from /usr, which shouldn't be there.

After strace-ing the pythons from /usr/local and the one from /usr, it
seems that python determines sys.prefix by first looking for os.py in
every path component of the interpreter executable and adding "/lib/
python2.4/os.py" to it (i.e. stat()-ing for /usr/local/inst/<version>/
bin/lib/python2.4/os.py, then /usr/local/inst/<version>/lib/python2.4/
os.py and so on) and only after it doesn't find os.py in any one of
those paths does it look at the compile-time PREFIX setting. When
doing this, it finds /usr/lib/python2.4/os.py (which belongs to the
python installed at /usr) and determines that sys.prefix is /usr,
which is wrong.

The only fix I could come up with is setting PYTHONHOME before running
python (which should be set differently for every version, and won't
work in scripts using a shebang line) or moving /usr/lib/python2.4 to
a different location (which is plain ugly). Is there a better way to
make python take its compile-time option of prefix and *not* try to
guess at runtime where it should be?

Thanks.



More information about the Python-list mailing list