Insane import behaviour and regrtest.py: heeelp

John J. Lee jjl at pobox.com
Sat Feb 5 17:37:40 EST 2005


I'm tearing my hair out at what seems like weird import behaviour I'm
getting from Python's stdlib test script, regrtest.py (not for the
first time: seem to have forgotten the resolution from last time, and
the time before, and the time before that, when this damn test script
of Python's had me screaming at the screen... I don't seem to be
learning my lesson here :( )

*Please* somebody inform me *what* affects what file Python will pick
up, other than:

0. Python executable

1. sys.path

2. current working directory

3. Directory in which script lives
 
4. Directory from which script was invoked

5. The contents of one's hard drive

Even with all of these constant (and not all of them should directly
affect the outcome in any case, I know), I get different modules
picked up from imports in two scripts, and I'm damned if I can see
why.

Here's the first script, just a test script:

import os, sys
sys.path = ['/hda/usr/local/buf/python/python/dist/src/Lib', '/home/john/lib/python', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', '/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages']
abstest = "test.test_urllib2"
print 'abstest', abstest
print 'sys.path', sys.path
print 'cwd', os.getcwd()
the_package = __import__(abstest, globals(), locals(), [])
print 'the_package.__path__', the_package.__path__

If I stick that script in my Python CVS Lib/test directory as blah.py,
then cd to Lib and:

$ python2.4 test/blah.py
abstest test.test_urllib2
sys.path ['/hda/usr/local/buf/python/python/dist/src/Lib', '/home/john/lib/python', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', '/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages']
cwd /hda/usr/local/buf/python/python/dist/src/Lib
*************
the_package.__path__ ['/hda/usr/local/buf/python/python/dist/src/Lib/test']


All as expected: I pick up Lib/test/test_urllib2.py -- that *******
business is coming from a print >> sys.stderr statement I inserted at
module level in that copy of the module (though the >> sys.stderr is
probably unnecessary, since IIRC regrtest does not rebind sys.stdout
when the -v option is given, as in my usage below).


But, when I run the second script, Lib/test/regrtest.py, with a
modified sys.path (see paragraph below) and the same print statements
as in my test script inserted:

$ python2.4 test/regrtest.py -f test/torun -v
test_urllib2
abstest test.test_urllib2
sys.path ['/hda/usr/local/buf/python/python/dist/src/Lib', '/home/john/lib/python', '/usr/local/lib/python24.zip', '/usr/local/lib/python2.4', '/usr/local/lib/python2.4/plat-linux2', '/usr/local/lib/python2.4/lib-tk', '/usr/local/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages']
cwd /hda/usr/local/buf/python/python/dist/src/Lib
the_package.__path__ ['/usr/local/lib/python2.4/test']

I get the test_urllib2.py from /usr/local/lib/python2.4 (note __path__
and lack of *****s), though everything else (sys.path, cwd, etc.)
remains the same as my test script above!

The only change I made to regrtest other than the print statements was
to add Lib to sys.path, so I pick up modules from CVS instead of the
installed 2.4 versions (yeah, I know I should build and install a
python2.5 executable from CVS, but I don't see how that's relevant here).

Why???  Help!

stupid-stupid-stupid-gnnnnhhhhh-<wink>-ly y'rs,


John



More information about the Python-list mailing list