confusion about package/module imports

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jan 1 18:16:40 EST 2008


Jugdish wrote:
> Thanks very much for your helpful response!
> 
>> You'll see that b is executed (making module __main__),
>> (1) it imports pkg.subpkg.a,
>> (2)   which is accomplished by importing pkg (successfully),
>> (3)                    then by importing pkg.subpkg
>> (4)                          which imports pkg.subpkg.a (successfully)
>> (5)                           and then imports pkg.subpkg.b
>> (6)                               which then attempts to import pkg.subpkg.a
> 
> What I'm not really understanding here is why this fails at lines (5)
> and (6). If pkg.subpkg.a has already been successfully imported at
> line (4), then (6) should be detected as a duplicate import and just
> be skipped, right? So the import at line (5) should succeed.

I'm sorry, I used shorthand.  While a module is being imported,
it only provisionally has a name.  Until subpkg is fully imported,
there is no module named pkg.subpkg.  At the root level (pkg, for
example), the module is provisionally added.  Further down the tree,
the module (such as that for pkg/subpkg/__init__.py) is only added
to the symbol table (the packages __dict__) when the module has been
completely imported.

-Scott



More information about the Python-list mailing list