Insane import behaviour and regrtest.py: heeelp

Tim Peters tim.peters at gmail.com
Sun Feb 6 16:53:17 EST 2005


[John J. Lee]
> 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}??

Can't guess from here, from what you've shown.  Obvious possibilities:

1. /usr/local/src/python/python/dist/src/Lib
    isn't on sys.path

2. /usr/local/src/python/python/dist/src/Lib
   is on sys.path but directory
   /usr/local/src/python/python/dist/src/Lib/test
   doesn't exist

3. /usr/local/src/python/python/dist/src/Lib
   is on sys.path and directory
   /usr/local/src/python/python/dist/src/Lib/test
   exists but one of these files doesn't exist:
   a. /usr/local/src/python/python/dist/src/Lib/test/test_cookielib.py
   b. /usr/local/src/python/python/dist/src/Lib/test/__init__.py

> 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

That doesn't look to be the same thing as the

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

you're asking about, right?

> Lib[0]$ ls test/test_cookielib* | grep -v 'patch\|latest\|~'
> test/test_cookielib.py
> test/test_cookielib.pyc

Since those files are apparenlty

    /hda/usr/local/buf/python/python/dist/src/Lib/test/test_cookielib.py
    /hda/usr/local/buf/python/python/dist/src/Lib/test/test_cookielib.pyc

and

    /hda/usr/local/buf/python/python/dist/src/Lib

isn't in your 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'
]

I don't understand why you think that "ls" output is (or should be) relevant.

> And the output:
> 
> Lib[0]$ python2.4 -u -vv test/regrtest.py -f test/torun -v 2>&1 | less
> [...long snip...]
> 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
...

See #2 and #3 above for some obvious possiblities not ruled out by
anything shown here.  I just don't see any mystery here, but might if
both of these displayed something interesting:

$ ls -l /usr/local/src/python/python/dist/src/Lib/test/test_cookielib.py
$ ls -l /usr/local/src/python/python/dist/src/Lib/test/__init__.py



More information about the Python-list mailing list