[Python-Dev] Looking for an "import" expert

M.-A. Lemburg mal@lemburg.com
Tue, 22 Aug 2000 10:01:40 +0200


Guido van Rossum wrote:
> 
> If the tests are run "the modern way" (python ../Lib/test/regrtest.py)
> then the test module is the script directory and it is on the path, so
> "import test_support" sees and loads a toplevel module test_support.
> Then "import test.test_support" sees a package test with a
> test_support submodule which is assumed to be a different one, so it
> is loaded again.
> 
> But if the tests are run via "import test.autotest" (or "import
> test.regrtest; test.regrtest.main()" the "import test_support" knows
> that the importing module is in the test package, so it first tries to
> import the test_support submodule from that package, so
> test.test_support and (plain) test_support are the same.
> 
> Conclusion: inside the test package, never refer explicitly to the
> test package.  Always use "import test_support".  Never "import
> test.test_support" or "from test.test_support import verbose" or "from
> test import test_support".

I'd rather suggest to use a different convention: *always* import
using the full path, i.e. "from test import test_support". 

This scales much better and also avoids a nasty problem with
Python pickles related to much the same problem Tim found here:
dual import of subpackage modules (note that pickle will always
do the full path import).
 
> This is one for the README!
> 
> I've fixed this by checking in a small patch to test_getopt.py and the
> corresponding output file (because of the bug, the output file was
> produced under verbose mode).
> 
> --Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://www.python.org/mailman/listinfo/python-dev

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/