__name__Variable in Python
Most of the programming languages use the main method or function as the starting point of execution in any program. But what about Python? Generally, the execution of a python program(script) starts from the very first line that is at the indentation level 0 of that program. However, when a python program is executed, before its execution a
__name__ variable is created. This variable can be used as an alternate for the main method in python.
__name__ variable is not available in Python 2.x version and was introduced in Python 3.0
__name__ is a built-in variable in python that stores the name of the current module. If the current module is executing then the
__name__ variable contains the value __main__ otherwise it simply contains the name of the module.
So, when a program or script is executing then its name variable will always have the value __main__ and if any python file is imported in any other program file then the
__name__ variable stores the name of the module. Thus it can be used to check whether the current script is running on its own or being imported somewhere else by combining it with an
if conditional statement.
Here __main__ is just a string that is used to check if the current module is running on its own or not. In the
__name__ variable, the double underscores on both sides of name is for indicating the python interpreter to recognize it as a special/reserved keyword.
__name__: Code Example
As specified above, when we execute a code file, for that code snippet, the value of
__name__ variable becomes __main__ as the code is being executed directly, without being imported into any other file.
For example: Here we have a 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.')
When we run python Script1.py then:
This function is in Script1. Called from Script1.
Now, let's create another python script file with name Script2.py and import the script Script1.py in it and try to call the function
something() defined in the script Script1.
Here's the code for Script2.py:
import Script1 if __name__=='__main__': Script1.something() print('Called from Script2.')
Now we run python Script2.py then:
Script1 is imported in some other file. This function is in Script1. Called from Script2.
import statement is executed for Script1, inside the Script2, during that the
__name__ variable had the value Script1(name of the module), but as the execution started with the script Script2, hence the
__name__ variable for it will have the value __main__.
__name__: Printing it's Value
To understand it better, let's print value of
__name__ variable at every execution step.
Here is the code for the python script Script1.py:
print('Value or __name__ variable: ' + __name__)
That's it, we won't include anything else in the script. And here is the code for the script Script2.py:
# import file Script1.py import Script1 print('Value or __name__ variable: ' + __name__)
Here is a live example:
NOTE: In the live example the scripts are main.py and Script1.py, the change in name of the scripts will not affect the functionality.