sneaky Python path

Andrew Dalke dalke at bioreason.com
Sat Jun 12 02:29:13 EDT 1999


I have a problem understanding how Python knows which files to
include in the default path.  I get different results if the
binary is run in its build directory as compared to outside of
it:

val> unsetenv PYTHONPATH
val> pwd
/var/tmp
val> ~/Python-1.5.1/python
'import site' failed; use -v for traceback
Python 1.5.1 (#23, Dec  8 1998, 05:12:14) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 
val> cp ~/Python-1.5.1/python .
val> ./python
Python 1.5.1 (#23, Dec  8 1998, 05:12:14) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 


When I do "~/Python-1.5.1/python -v" it says:
'import site' failed; traceback:
Traceback (innermost last):
  File "/home/u03/dalke/Python-1.5.1/Lib/site.py", line 60, in ?
    import sys, os
  File "/home/u03/dalke/Python-1.5.1/Lib/os.py", line 35, in ?
    import posixpath
SystemError: bad argument to internal function


  I think the problem arises from getpath.c in search_for_prefix
where it appends "Lib" to the path if the code is in the build
directory (as determined by looking for Modules/Setup).  That's
the only reason place I can find where "Lib" can be added to
the path.  To double check, if I move Setup to Setup.orig, then
everything is okay

val> pwd
/home/u03/dalke/Python-1.5.1
val> cd Modules/
val> mv Setup Setup.orig
val> pushd /usr/tmp
/usr/tmp ~/Python-1.5.1/Modules 
val> ~/Python-1.5.1/python 
Python 1.5.1 (#26, Jun 11 1999, 23:54:47) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 

  I found this problem when I tried running a "make test" on 
the distribution, and it failed saying:

% make test
   [...some lines omitted...]
        rm -f ./Lib/test/*.py[co]
        PYTHONPATH= ./python ./Lib/test/regrtest.py 
'import site' failed; use -v for traceback
Traceback (innermost last):
  File "./Lib/test/regrtest.py", line 193, in ?
    sys.exit(main())
  File "./Lib/test/regrtest.py", line 60, in main
    tests = args or findtests()
  File "./Lib/test/regrtest.py", line 104, in findtests
    testdir = findtestdir()
  File "./Lib/test/regrtest.py", line 160, in findtestdir
    testdir = os.path.dirname(file) or os.curdir
AttributeError: path


  I'm not sure what changed between then and now.  All I can
think of is that my home account got moved after a recent server
crash, but I can't see any place in the code where that would
cause a problem, and my home directory isn't even stored in
the .o files anywhere.

  Also, I've found a workaround by copying a valid posixpath.pyc
into the Lib directory.  Watch this:

val> rm Lib/*.py
val> ./python 
'import site' failed; use -v for traceback
Python 1.5.1 (#26, Jun 11 1999, 23:54:47) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 
val> cp /usr/local/lib/python1.5/posixpath.pyc Lib
val> ./python 
Python 1.5.1 (#26, Jun 11 1999, 23:54:47) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam

 So I'm stumped, stymied and just plain old confused.  Python is
doing something sneaky and I can't figure it out :(

						Andrew
						dalke at bioreason.com




More information about the Python-list mailing list