Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is executed only one module import instead of 2?

I suppose to have 3 files in the folder project as showed following:

project
 |- module_b.py
 |- script_B1.py
 |- script_B2.py

Content of 3 python files

The content of module_b.py is:

print("Inside module_b")

The content of script_B1.py is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import module_b
print("Inside script_B1")

The content of script_B2.py is:

import module_b
import script_B1
print("Inside script_B2")

Output of execution of 2 scripts

The output of the execution of script_B1.py is:

Inside module_b
Inside script_B1

The output of script_B2.py is:

Inside module_b
Inside script_B1
Inside script_B2

Question about the output of script_B2.py

In the execution of script_B2.py the module module_b.py is imported first by script_B2.py and after by script_B1.py. So I was thinking that the output of the execution of script_B2.py would be:

Inside module_b
Inside module_b
Inside script_B1
Inside script_B2

My question is: why the output of execution of script_B2.py contains only one string Inside module_b?

>Solution :

A module that has already been imported is not loaded again.

This question is a duplicate of this question: What happens when a module is imported twice?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading