Loading a file only once into an object and being able to access it from other modules

Peter Hansen peter at engcorp.com
Tue Dec 7 20:12:32 CET 2004

Philippe C. Martin wrote:
> Thank you all for your answers, I guess I would not have made Python 101:-)
> As far as I was concerned, importing a module twice would have resulted in 
> loading the file twice.

Background on that:  importing the module the first time causes the
code in it to be executed (i.e. all those def statements and such)
and the resulting module gets a reference stored in sys.modules
(which is a dictionary you might want to examine at the prompt
to see how it looks).  Any subsequent "import" of the same module
will just retrieve a reference to it from sys.modules, so the
second and later imports are effectively instantaneous...


More information about the Python-list mailing list