Import caches names (was Re: Problems with importing; must do it twice?)

Peter Hansen peter at engcorp.com
Mon Jun 17 22:19:43 EDT 2002


Aahz wrote:
> 
> Python caches import names even when the import fails
> (protects against circular references)

I thought about this for a while and suppose I can see how it
protects against circular reference problems (I'm guessing it
creates a new module object and stores it in sys.modules 
immediately upon encountering the import statement, and only 
subsequently does it try to import and execute the contents),
but what I don't understand is why it doesn't clean up after
itself in the case of a failed import.  

Should it not be considered a bug that a failed import leaves 
a broken module around?  

Is it possible to redefine this so that it cleanly removes the 
newly created reference if an import fails?  

Or would it then be a problem that circularly imported modules
might now have references back to a module which appears never
to have been imported (because it failed, and was deleted)?

And have I now answered all my questions?  :-)

-Peter



More information about the Python-list mailing list