Import problem

Fredrik Lundh fredrik at effbot.org
Wed Dec 20 14:58:41 EST 2000


David Allen wrote:
> What else can cause this?  How is it that you can
> from modulename import * and then NOT have access
> to the things in that namespace?

when Python imports a module, it creates an empty
module namespace.  It then executes the module code
in that namespace.

if the module is already present in the sys.modules cache,
Python doesn't load it again.  Instead, it returns a reference
to the existing module.

"from X import *" imports the module X, and creates
references in the current name space to all public symbols
defined by that module *at that time*.

in your case, you end up calling "from X import *" before
module X is fully populated.

> Any help on figuring this out would be much appreciated.

import rule #4: only use "from import" if you know exactly
what you're doing.

(corollary: if you're using "from import", and get an error
you don't understand, you shouldn't be using it ;-)

for more info, see:

    http://effbot.org/guides/import-confusion.htm

if you have trouble understanding names and references,
this might help:

    http://effbot.org/guides/python-objects.htm

</F>





More information about the Python-list mailing list