Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python optimize modules when they are imported multiple times?

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 
like image 283
JimB Avatar asked Nov 17 '08 16:11

JimB


People also ask

How many times does a module get loaded when imported multiple times in Python?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

Does Python import module multiple times?

So each module is imported only one time.

What does Python do to optimize the execution time with various imported modules?

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.

What happens when a Python module is imported?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.


1 Answers

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.

like image 145
Toni Ruža Avatar answered Oct 21 '22 02:10

Toni Ruža