Welcome to a tutorial on the _name_ variable in Python. The __name__ variable is a special variable where the name of the current python script/module being executed is stored. However, it is not available in Python 2.x version and was introduced in the python 3.0 version.
In python, the __name__ variable is assigned value __main__ for the present python script or module, at the time of execution.
The __name__ is a built-in variable in python that stores the name of the current module/script that is executed. But, in the case where the current module is executing, then the __name__ variable is assigned the value __main__, else, it simply contains the name of the module or script.
Check out the example below for a better understanding, here we will create a python script with the test.py.
# defining a function
def something():
print('Value of __name__:', __name__)
something()
Output:
Value of __name__: __main__
If executed, the value of the __name__ variable is set as __main__.
Also, we will create another python script main.py.
# importing test.py
import test
test.something()
Output:
Value of __name__: test
The output of the example above shows that the value of the __name__ variable is tested because the __name__ variable of the test.py module was printed.
Here, in other to specify some code in any python script that should be executed only when that script is executed directly; the if statement with the condition __name__ == __main__ can be used.
# importing test.py
import test
if __name__ == __main__:
test.something()
From the above example, the __main__ is simply a string that is used to check if the current module/script is running on its own or not. In the __name__ variable, the double underscores on both sides of the name are to alert the python interpreter to recognize it as a special/reserved keyword.
Just as in the example above, if we execute a code file, for that code snippet, the value of __name__ variable changes to __main__ as the code is being executed directly, without being imported into any other file.
Check out this code file Script1.py,
# defining a function
def something():
print('This function is in Script1.')
if __name__=='__main__':
something()
print('Called from Script1.')
else:
print('Script1 is imported in some other file.')
Then, we will run python Script1.py then:
Output:
This function is in Script1.
Called from Script1.
Let's try to create another python script file with the name Script2.py, import the script Script1.py in it and try to call the function something() defined in the script Script1.
Check the code for Script2.py:
import Script1
if __name__=='__main__':
Script1.something()
print('Called from Script2.')
Output:
Script1 is imported in some other file.
This function is in Script1.
Called from Script2.
From the example above, if the import statement is executed for Script1, inside the Script2, during that the __name__ variable had the value Script1(name of the module), however, since the execution started with the script Script2, thus, the __name__ variable for it will have the value __main__.
Let's print a value of the __name__ variable at every execution step so we can understand.
Check out the example for the python script Script1.py:
print('Value or __name__ variable: ' + __name__)
This is what we want, now, we will do anything else in the script. Check out this code for the script Script2.py:
# import file Script1.py
import Script1
print('Value or __name__ variable: ' + __name__)
Conclusively, the majority of the programming languages make use of the main method or function as the starting point of execution in any program.