Binance Exchange

Learning Python from scratch #02: modules, imports and __name__ variable

A Python program, unlike almost all other programming languages, has not a main method from which its execution starts. When running a Python module, the interpreter starts to execute the code that is on level 0 indentation.
Let’s create a simple module and try to run it from the command line:

Python module

As we can see the execution starts from level 0 indentation code. When a form is directly run, the interpreter sets a special variable called __ name __ to the value __ main __ .
This can be verified by changing our simple example as follows:

Python __name__ variable

Through the shell invocation of the python command, however, is not the only way in which a module can be executed. Another way is through import statement. When a module is imported into another one, it is run by the interpreter directly from the import statement.
Let’s see an example. We create two different modules, and we save the first one in a file called python-02.py

import my_2nd_module

def my_function():
    print("Inside the function")

print("Program starts")
print(4 + 5)
print(__name__)

and the second one in a file called my_2nd_module.py

def function_inside_module():
	print("we are here")

print("imported module")
function_inside_module()

As we can see the first module imports the second one. Trying to run the module “python-02.py” we get the following result:

Python modules import example

Looking at generated output we see that statements of the imported module are executed before the module ran from shell. This is because, as mentioned, the import statement directly runs the imported module.
Let’s also add in the “my_2nd_module.py” module a statement printing the value of the special variable __ name __ .

def function_inside_module():
	print("we are here")

print("imported module")
function_inside_module()
print(__name__)

The result of this new run is shown in the following figure:
Python modules import __name__ variable

In this case, as value of the variable __name__ in the imported module we get the value “ my_2nd_module “. This is because the module was imported and not directly run as a main module.

The special variable __name__ then assumes the values:

  • __ main __ if the module is executed directly
  • module name if the module is not run directly but is imported from another module

This allows us to determine, within a module, what we want to run every time and what we want to run only when the module is executed as the main module.
Let’s take another example. Edit the “my_2nd_module.py” module by inserting the check, at level 0 indentation, that the value of the __name__ variable is equal to “__main__”. In this way we say to the module to execute the statements in the “if” block only in case the module is executed directly and to skip them in case it is imported into another module.

def function_inside_module():
	print("we are here")

if __name__ == "__main__":
	print("imported module")
	function_inside_module()
	print(__name__)

We also modify the “python-02.py” module by including a call to the “function_inside_module()” function defined in the file “my_2nd_module.py”.

import my_2nd_module

def my_function():
    print("Inside the function")

print("Program starts")
print(4 + 5)
print(__name__)
my_2nd_module.function_inside_module()

Running again the “python-02.py” module from command line we get:

Python check on __name__ variable

In this case, as we expected, the module is imported and the functions defined in it are available to “caller” module, but the code inside the if block is not executed. This is because the __name__ variable contains the module name and not the value “__main__”, since it was imported and not run directly.

There are a couple of things we need to look at in the code of the two module we wrote:

  • When we import a module we have to provide his name without indicating the .py extension. Otherwise we will get an error:
  • Python module import error

  • To invoke a function defined in an imported module we have to use the syntax nome_del_modulo.funzione()

Again, if you are not using the correct syntax, you get error:

Python function invocation error

Leave a Reply

Your email address will not be published. Required fields are marked *