70

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
Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
JimB
  • 104,193
  • 13
  • 262
  • 255

6 Answers6

95

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:

import MyLib
import ReallyBigLib

Relevant documentation on the import statement:

https://docs.python.org/2/reference/simple_stmts.html#the-import-statement

Once the name of the module is known (unless otherwise specified, the term “module” will refer to both packages and modules), searching for the module or package can begin. The first place checked is sys.modules, the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import.

The imported modules are cached in sys.modules:

This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. Note that removing a module from this dictionary is not the same as calling reload() on the corresponding module object.

Toni Ruža
  • 7,462
  • 2
  • 28
  • 31
  • 3
    However, if you want to reimport the module, then you can use the reload() function. This could be if the module has been changed, for instance. – Matthew Schinckel Nov 17 '08 at 22:43
44

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:

   # module/file a.py
   print "Hello from a.py!"
   import b

   # module/file b.py
   print "Hello from b.py!"
   import a

There is no loop: there is only a cache lookup.

>>> import b
Hello from b.py!
Hello from a.py!
>>> import a
>>>

One of the beauties of Python is how everything devolves to executing a script in a namespace.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
  • "One of the beauties of Python is how everything devolves to executing a script in a namespace." Except, perhaps, Python's compiled binaries from C code? – flow2k Apr 25 '19 at 23:54
  • It would be interesting to document the binaries' logic in simplified Python. In theory, the documentation could execute to prove the C code. – Charles Merriam May 04 '19 at 16:48
9

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.

Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
8

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

Munhitsu
  • 1,014
  • 1
  • 9
  • 16
  • 19
    For anyone reading this after 2018: The issue seems to be fixed according to the link. – oli Oct 04 '18 at 00:08
3

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.

babbageclunk
  • 8,523
  • 1
  • 33
  • 37
2

It is the same performancewise. There is no JIT compiler in Python yet.

Marko
  • 30,263
  • 18
  • 74
  • 108