Every module in Python has an attribute called __name__. The value of __name__ attribute is __main__ when the module is run directly, like python my_module.py. Otherwise (like when you say import my_module) the value of __name__ is the name of the module.
Small example to explain in short.
Script test.py
apple = 42
def hello_world():
print("I am inside hello_world")
if __name__ == "__main__":
print("Value of __name__ is: ", __name__)
print("Going to call hello_world")
hello_world()
We can execute this directly as
python test.py
python test.py
Output
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world
Now suppose we call the above script from another script:
Script external_calling.py
import test
print(test.apple)
test.hello_world()
print(test.__name__)
When you execute this,
python external_calling.py
python external_calling.py
Output
42
I am inside hello_world
test
42
I am inside hello_world
test
So, the above is self-explanatory that when you call test from another script, if loop __name__ in test.py will not execute.