Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python import multiple times

Tags:

I suppose this is a general question so sorry if not posted in the right place.

Say for instance, I have a function a which imports os. If I was to call this function from another file multiple times I am assuming that the import would be done multiple times as well? Is there a way to only import the module if its not already present?

Basically, I have a class which calls multiple functions imported from various files, instead of importing the whole file I thought it would be easier to import just the function but now I am wondering if I am going to give myself headaches in the long run with excess imports.

like image 974
JONAS402 Avatar asked May 06 '16 08:05

JONAS402


People also ask

Does Python import multiple times?

So each module is imported only one time. To better understand import mechanics I would suggest to create toy example. So import really happens only once. You can adjust this toy example to check cases that are interesting to you.

Can we import a module twice?

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.

How many times does a module gets loaded when imported multiple times?

A module code is evaluated only the first time when imported. If the same module is imported into multiple other modules, its code is executed only once, upon the first import. Then its exports are given to all further importers. The one-time evaluation has important consequences, that we should be aware of.

What is Python import sys?

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.


1 Answers

As described in python documentation, when python see some import statement it does the following things:

  • checks some global table if module is already imported
    • if module is not imported python imports it, creates module object and puts newly created module object to the global table
    • if module is imported python just gets module object
  • when python has module object it binds it to the name you chose
    • if it was import foo name for module foo will be foo
    • if it was import foo as bar name for module foo will be bar
    • if it was from foo import bar as baz python finds function (or whatever) bar in module foo and will bind this function to name baz

So each module is imported only one time.

To better understand import mechanics I would suggest to create toy example.

File module.py:

print("import is in progress")

def foo():
    pass

File main.py:

def foo():
    print("before importing module")
    import module
    module.foo()
    print("after importing module")

if __name__ == '__main__':
    foo()
    foo()

Put above files to the same directory. When module.py is being imported it prints import is in progress. When you launch main.py it will try to import module several times but the output will be:

before importing module
import is in progress
after importing module
before importing module
after importing module

So import really happens only once. You can adjust this toy example to check cases that are interesting to you.

like image 62
Dmitry Ermolov Avatar answered Oct 18 '22 07:10

Dmitry Ermolov