[Python-Dev] Re: Christmas Wishlist

Guido van Rossum guido at python.org
Mon Dec 15 13:38:01 EST 2003


> I've seen a couple people get bitten by the fact that a module gets 
> loaded twice if there are separate local & global imports for it:
> 
>      % mkdir pkg
> 
>      % touch pkg/__init__.py
> 
>      % cat >pkg/a.py
>      print 'A is being imported (not re-used)'
>      class A: pass
> 
>      % cat >pkg/b.py
>      from a import A as LocalA
>      from pkg.a import A as GlobalA
>      print isinstance(LocalA(), GlobalA)
>      print isinstance(GlobalA(), LocalA)
> 
>      % PYTHONPATH=.; python pkg/b.py
>      A is being imported (not re-used)
>      A is being imported (not re-used)
>      0
>      0
> 
> Since pkg/a gets loaded twice, we end up with two versions of class A, 
> which are not compatible.  In practice this usually comes up if a 
> package uses local imports between submodules, and then an outside user 
> uses a global import to get a submodule.

That's actually a slightly different problem; sys.path should not
include any directories that are also packages on other directories in
sys.path.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list