[Python-Dev] How *does* Python determine sys.prefix?

Greg Ward gward@mems-exchange.org
Tue, 11 Apr 2000 22:10:18 -0400


Ooh, here's a yucky problem.  Last night, I installed Oliver Andrich's
Python 1.5.2 RPM on my Linux box at home, so now I have two Python
installations there:

  * my build, in /usr/local/python and /usr/local/python.i86-linux
    (I need to test Distutils in the prefix != exec_prefix case)

  * Oliver's RPM, in /usr

I have a symlink /usr/local/bin/python pointing to
../../python.i86-linux/bin/python, and /usr/local/bin is first in my
path:

  $ ls -lF `which python`
  lrwxrwxrwx   1 root     root           30 Aug 28  1999 /usr/local/bin/python -> ../python.i86-linux/bin/python*

Since I installed the RPM, /usr/local/bin/python reports an incorrect
prefix:

  $ /usr/local/bin/python
  Python 1.5.2 (#1, Jun 20 1999, 19:56:42)  [GCC 2.7.2.3] on linux2
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> import sys ; sys.prefix, sys.exec_prefix
  ('/usr', '/usr/local/bin/../python.i86-linux')

Essentially the same thing if I run it directly, not through the
symlink:

  $ /usr/local/python.i86-linux/bin/python
  Python 1.5.2 (#1, Jun 20 1999, 19:56:42)  [GCC 2.7.2.3] on linux2
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> import sys ; sys.prefix, sys.exec_prefix
  ('/usr', '/usr/local/python.i86-linux')

/usr/bin/python gets it right, though:

  $ /usr/bin/python
  Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
  (egcs-1.1.1  on linux2
  Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
  >>> import sys ; sys.prefix, sys.exec_prefix
  ('/usr', '/usr')

This strikes me as a pretty reasonable and straightforward way to have
multiple Python installations; if Python is fooled into getting the
wrong sys.prefix, then the Distutils are going to have a much tougher
job!  Don't tell me I have to write my own prefix-finding code now...

(And no, I have not tried this under 1.6 yet.)

Damn and blast my last-minute pre-release testing... I should have just
released the bloody thing and let the bugs fly.  Oh hell, I think I will
anyways.

        Greg
-- 
Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange / CNRI                           voice: +1-703-262-5376
Reston, Virginia, USA                            fax: +1-703-262-5367