Importing and namespace visibility

Fredrik Lundh fredrik at pythonware.com
Mon May 16 02:44:32 EDT 2005


"jean-marc" wrote:

> BUT, of all this I thought that if you import module1, then module2
> (into __main__), objects from module1 would be available to objects of
> module2 which came (into memory space) after module1 was loaded. This
> does not seem to be the case, and module2 requires an 'import module1'
> statement in its own file to see this last module's objects.  This is
> not the recursive situation that was a pitfall Fredrik was evoking.
> What am I missing here???

importing something into a module only makes the names available in
that module, not in every other module.  (import is not some kind of
global include).  for the details, read the "There are Many Ways to
Import a Module" section under:

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

(and as mentioned under "What Does Python Do to Import a Module",
importing a module twice doesn't load the module again; it only makes
the name(s) available to the importing module).

reading the following sections in the language reference may also help:

    http://docs.python.org/ref/naming.html
    http://docs.python.org/ref/import.html

</F>






More information about the Python-list mailing list