Question: Importing the same module under different names

brueckd at tbye.com brueckd at tbye.com
Mon Jun 3 21:06:33 CEST 2002


On Mon, 3 Jun 2002, Andreas Jung wrote:

> Usually Python imports a module 'foo' only once when one uses
> multiple 'import foo' statements. However Python load the 
> module when one uses packages e.g.
> 
> packages/__init.py
>         /foo.py
> 
> 
> When PYTHONPATH is set to the 'packages' directory then

This defeats the purpose of having packages in the first place.

> import foo
> import packages.foo 
[snip]
> loads the foo module twice. How does Python determine in this case
> to load the module or not?

IMO, setting the PYTHONPATH to include a subtree of your package directory 
is the wrong thing to do. Is there a case when you really need to do this? 
It seems akin to:

ln -s foo.py bar.py

and then noticing that

import foo, bar

causes the module to load twice, i.e. it's too much to hope that Python 
will figure out your trickery! :-)

-Dave






More information about the Python-list mailing list