Module import information

Peter Hansen peter at engcorp.com
Tue Mar 21 09:54:52 EST 2006


Eric White wrote:
> Is it possible to determine the name of the module that invoked import 
> from within the imported module?

Almost anything is possible in Python.  On the other hand, some things 
are probably impractical, and others ill-advised.  Perhaps you can 
describe your requirements, *why* you want to do this, to provide some 
context for the answers.

Also, consider that the effect of the import statement is different 
after the first import.  When an already-imported module is imported by 
another module (i.e. the second time "import xxx" is executed for your 
module), the code is not reloaded from the .pyc file, but instead a 
reference is retrieved from sys.modules where it was cached when the 
first import was executed.  What are you expecting to happen in that case?

-Peter




More information about the Python-list mailing list