[Python-Dev] Re: Christmas Wishlist

Edward Loper edloper at gradient.cis.upenn.edu
Mon Dec 15 13:44:56 EST 2003

Barry Warsaw <barry at python.org> wrote:
> Actually, if truth be told, I'd love to just ban local imports.  I
> understand that might be controversial though <wink>.

+1 (asuming that you're talking about package-local imports).

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)

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.


More information about the Python-Dev mailing list