Insane import behaviour and regrtest.py: heeelp

John J. Lee jjl at pobox.com
Sun Feb 6 15:10:58 EST 2005


Tim Peters <tim.peters at gmail.com> writes:

> [John J. Lee]
> > ...
> > I tried it, and I get the same results as before (the test modules from my
> > installed copy of Python are picked up instead of the local copies in
> > my CVS checkout's Lib/test, apparently entirely contrary to sys.path).
> 
> Try running Python with -vv (that's two letter "v", not one letter
> "w").  That will display a long-winded account of everything Python
> tries in order to satisfy an import.

Thanks.

I'm still puzzled, though.  Reading the -vv output, I see that when
importing test_cookielib (which is currently the only line in my
regrtest.py -f file), Python doesn't appear to look in Lib/test to
find module "test.test_cookielib" (the actual string passed by
regrtest.py to __import__): see the output below.

Why does Python not look in

/usr/local/src/python/python/dist/src/Lib/test

for test_cookielib.py{,c,o}??


Snippet to show print statements I added to my regrtest.py :

.     try:
.         save_stdout = sys.stdout
.         try:
.             if cfp:
.                 sys.stdout = cfp
.                 print test              # Output file starts with test name
.             if test.startswith('test.'):
.                 abstest = test
.             else:
.                 # Always import it from the test package
.                 abstest = 'test.' + test
.+            print >> sys.stderr, ">>>>>>>sys.path", sys.path
.             the_package = __import__(abstest, globals(), locals(), [])
.+            print >> sys.stderr, ">>>>>>>the_package.__path__", the_package.__path__
.             the_module = getattr(the_package, test)
.             # Most tests run to completion simply as a side-effect of

Only other change to regrtest.py:

. if __name__ == '__main__':
.     # Remove regrtest.py's own directory from the module search path.  This
.     # prevents relative imports from working, and relative imports will screw
.     # up the testing framework.  E.g. if both test.test_support and
.     # test_support are imported, they will not contain the same globals, and
.     # much of the testing framework relies on the globals in the
.     # test.test_support module.
.     mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
.     i = pathlen = len(sys.path)
.     while i >= 0:
.         i -= 1
.         if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
.             del sys.path[i]
.     if len(sys.path) == pathlen:
.         print 'Could not find %r in sys.path to remove it' % mydir
.+    lib = "/usr/local/src/python/python/dist/src/Lib"
.+    sys.path.insert(0, lib)
.-    main()
.+    main(testdir=lib)


Here's what I've got in my CVS checkout:

Lib[0]$ pwd
/hda/usr/local/buf/python/python/dist/src/Lib
Lib[0]$ ls test/test_cookielib* | grep -v 'patch\|latest\|~'
test/test_cookielib.py
test/test_cookielib.pyc


And the output:

Lib[0]$ python2.4 -u -vv test/regrtest.py -f test/torun -v 2>&1 | less
[...long snip...]
# trying /usr/local/lib/python2.4/lib-tk/time.py
# trying /usr/local/lib/python2.4/lib-tk/time.pyc
# trying /usr/local/lib/python2.4/lib-dynload/time.so
dlopen("/usr/local/lib/python2.4/lib-dynload/time.so", 2);
import time # dynamically loaded from /usr/local/lib/python2.4/lib-dynload/time.so
test_cookielib
>>>>>>>sys.path ['/usr/local/src/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']
# trying /usr/local/lib/python2.4/test/test_cookielib.so
# trying /usr/local/lib/python2.4/test/test_cookielibmodule.so
# trying /usr/local/lib/python2.4/test/test_cookielib.py
import test.test_cookielib # from /usr/local/lib/python2.4/test/test_cookielib.py
# can't create /usr/local/lib/python2.4/test/test_cookielib.pyc
# trying /usr/local/lib/python2.4/test/re.so
# trying /usr/local/lib/python2.4/test/remodule.so
# trying /usr/local/lib/python2.4/test/re.py
# trying /usr/local/lib/python2.4/test/re.pyc
# trying /usr/local/lib/python2.4/test/time.so
# trying /usr/local/lib/python2.4/test/timemodule.so
# trying /usr/local/lib/python2.4/test/time.py
# trying /usr/local/lib/python2.4/test/time.pyc
# trying /usr/local/lib/python2.4/test/test.so
# trying /usr/local/lib/python2.4/test/testmodule.so
# trying /usr/local/lib/python2.4/test/test.py
# trying /usr/local/lib/python2.4/test/test.pyc
>>>>>>>the_package.__path__ ['/usr/local/lib/python2.4/test']
# trying /usr/local/lib/python2.4/test/test_sets.so
# trying /usr/local/lib/python2.4/test/test_setsmodule.so
# trying /usr/local/lib/python2.4/test/test_sets.py
# /usr/local/lib/python2.4/test/test_sets.pyc matches /usr/local/lib/python2.4/test/test_sets.py
import test.test_sets # precompiled from /usr/local/lib/python2.4/test/test_sets.pyc
# trying /usr/local/lib/python2.4/test/operator.so
# trying /usr/local/lib/python2.4/test/operatormodule.so
# trying /usr/local/lib/python2.4/test/operator.py
# trying /usr/local/lib/python2.4/test/operator.pyc
# trying /usr/local/src/python/python/dist/src/Lib/operator.so
[...long snip...]


John



More information about the Python-list mailing list