If a large module is loaded by some submodule of your code, is there any benefit to referencing the module from that namespace instead of importing it again?
For example: I have a module MyLib, which makes extensive use of ReallyBigLib. If I have code that imports MyLib, should I dig the module out like so
import MyLib
ReallyBigLib = MyLib.SomeModule.ReallyBigLib
or just
import MyLib
import ReallyBigLib
As others have pointed out, Python maintains an internal list of all modules that have been imported. When you import a module for the first time, the module (a script) is executed in its own namespace until the end, the internal list is updated, and execution of continues after the import statement.
Try this code:
There is no loop: there is only a cache lookup.
One of the beauties of Python is how everything devolves to executing a script in a namespace.
WARNING: Python does not guarantee that module will not be initialized twice. I've stubled upon such issue. See discussion: http://code.djangoproject.com/ticket/8193
It is the same performancewise. There is no JIT compiler in Python yet.
It makes no substantial difference. If the big module has already been loaded, the second import in your second example does nothing except adding 'ReallyBigLib' to the current namespace.
The internal registry of imported modules is the
sys.modules
dictionary, which maps module names to module objects. You can look there to see all the modules that are currently imported.You can also pull some useful tricks (if you need to) by monkeying with
sys.modules
- for example adding your own objects as pseudo-modules which can be imported by other modules.Python modules could be considered as singletons... no matter how many times you import them they get initialized only once, so it's better to do:
Relevant documentation on the import statement:
https://docs.python.org/2/reference/simple_stmts.html#the-import-statement
The imported modules are cached in sys.modules: