Please help me to understand why this is happening.
When you import a module you are in fact executing it, so every statement in the module will get run. If it is just function
defs then you will just add those functions to the namespace, but not actually run the code they contain. However if it contains
To summarise when you import a module you execute everything in that module.
This is also why you see very often code that looks like
if __name__ == "__main__" at the bottom of many modules. If the module gets imported the
if condition won’t be satisfied, but if the module is executed directly as a script then the
if condition will be satisfied and the code will run.
As a side note you should do your best to avoid
from module import * – it will make debugging ten times harder as it makes it impossible for the computer (and the human) to know where items in its name space came from.