Loading and initializing a module
Whenever the Python interpreter interacts with an import
or equivalent statement, it does three operations, which are described in the next sections.
Loading a module
The Python interpreter searches for the specified module on a sys.path
(to be discussed in the Accessing packages from any location section) and loads the source code. This has been explained in the Learning how import works section.
Setting special variables
In this step, the Python interpreter defines a few special variables, such as __name__
, which basically defines the namespace that a Python module is running in. The __name__
variable is one of the most important variables.
In the case of our example of the calcmain1.py
, mycalculator.py
, and myrandom.py
modules, the __name__
variable will be set for each module as follows:
There are two cases of setting the __name__
...