Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python multiple imports for a common module

Tags:

python

I am working on a project wherein I need to use a third party module in different project files(.py files). The situation is like this.

I have a file "abc.py" which imports third party module "common.py". There are couple of other files which also import "common.py". All these files are also imported in main project file "main.py".

It seems redundant to import same module in your project multiple times in different files since "main.py" is also importing all the project files.

I am also not sure how the size of the project gets affected by multiple import statements.

Can someone pls help me in making things bit simpler.

like image 298
Raj Avatar asked Jan 14 '11 08:01

Raj


People also ask

What happens if I import the same module twice?

If multiple modules imports the same module then angular evaluates it only once (When it encounters the module first time). It follows this condition even the module appears at any level in a hierarchy of imported NgModules.

Can you import more than one module in Python?

You can write multiple modules separated by commas after the import statement, but this is not recommended in PEP8. Imports should usually be on separate lines. If you use from to import functions, variables, classes, etc., as explained next, you can separate them with a comma.

What happens when a module is imported twice Python?

What happens if a module is imported twice? The module is only loaded the first time the import statement is executed and there is no performance loss by importing it again.

How many times is a module imported Python?

They are executed only the first time the module name is encountered in an import statement. 1 (They are also run if the file is executed as a script.) Each module has its own private namespace, which is used as the global namespace by all functions defined in the module.


2 Answers

Importing only ever loads a module once. Any imports after that simply add it to the current namespace.

Just import things in the files you need them to be available and let Python do the heavy-lifting of figuring out loading the modules.

like image 190
Amber Avatar answered Oct 05 '22 10:10

Amber


Yes, you are right, this behavior really exists in Python. Namely, if user code tries to import the same module in different ways, for example - import a and import A.a (where a.py file is located into A package and the first import is done from within the A package while the other import comes as from outside).

This can easily happen in real life, especially for multi-level packaged Python projects.

I have experienced a side-effect of such behavior, namely command isinstance does not work when an object is checked against a class that is defined in module that was imported in such way.

The solution I can think about is to redefine the __builtin__. __ import__ function to perform its work more intelligently.

like image 28
Alexey Avatar answered Oct 05 '22 10:10

Alexey