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:

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?

Leave a Reply