Does Python Optimize Modules When They Are Imported Multiple Times

Does python optimize modules when they are imported multiple times?

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.

How safe is it to import a module multiple times?

Yes, you can import module as many times as you want in one Python program, no matter what module it is. Every subsequent import after the first accesses the cached module instead of re-evaluating it.

Behavior of import in python

Well, let's experiment:

tbi.py

print("Hello, World!")

file1.py

import tbi
print("This is file1")

file2.py

import file1
import tbi
print("This is file2")

And now, when we run file2, we see:

$ python file2.py
Hello, World!
This is file1
This is file2

So the answer to your question is yes, python modules are executed once only. If tbi.py were executed twice, we would have seen "Hello World" printed twice. It's logical, then, to conclude that the attributes of the file are set the first time that file is imported.

Furthermore, more experimentation can show you that, if I put a global variable in tbi, and both file1 and file2 modified it, they would both be modifying the same object. This is visible in a number of built-in packages: for example, changing the value of sys.stdout (the file descriptor for standard output, and a global variable specified when the sys module is first loaded) changes it for the entire program, not just the file that modified it.


If you're worried about this sort of thing causing errors, the best thing to do is to not use global variables - instead, use classes and assign them default values when constructed.

Python: Optimizing Module Imports

No. The python documentation states:

The import statement combines two operations; it searches for the
named module, then it binds the results of that search to a name in
the local scope.

If the import occurs at the top of a module, local scope will be global - i.e. local to the module.

On the plus side, normally imports will only occur once. Python will search the cache for imports before carrying out an import and will create a reference to a previously imported module if it finds it. This saves have to load a second copy - there's no harm (generally) in having the import statement in both modules.

The first place checked during import search is sys.modules. This
mapping serves as a cache of all modules that have been previously
imported, including the intermediate paths.

Additionally, from the FAQs:

How do I share global variables across modules?

The canonical way to share information across modules within a single
program is to create a special module (often called config or cfg).
Just import the config module in all modules of your application;

Python - optimize by not importing at module level?

No, don't do this. In a normal python execution environment on the web (mod_wsgi, gunicorn, etc.) when your process starts those imports will be executed, and then all subsequent requests will not re-execute the script. If you put the imports inside the functions they'll have to be processed every time the function is called.

What is the side effect of importing something inside a loop in python?

Just move the import out of the loop. You are correct in that this is causing Python some extra work each iteration.

A Python import statement does two things:

  • Check if the referenced module is already loaded, and if not, load it.
  • Bind (assign) one or more names.

So each loop iteration Python checks if the module is already loaded (roughly equivalent to checking 'Levenshtein._levenshtein' in sys.modules), then sets the name distance.

Provided nothing else in the loop sets the name distance to anything else, you can safely move the import out of the loop and save yourself those checks as well. If there is such code, then refactor it to not use the same name, and move the import out of the loop.

memory impact and scope/lifespan of imported modules python

You can actually unload a module from python, it will be garbage collected if it is not referenced anymore :

del sys.modules["mymodule"]
del mymodule


Related Topics



Leave a reply



Submit