__name__ is a special variable in which the name of the current python script/module being executed is stored.
__name__ variable is not available in Python 2.x version and was introduced in Python 3.0 version.
In python, the
__name__ variable is assigned value __main__ for the current python script or module, when it is being executed.
__name__ is a built-in variable in python that stores the name of the current module/script being executed. If the current module is executing then the
__name__ variable is assigned the value __main__ otherwise it simply contains the name of the module or script.
So, when a python script is executing then its
__name__ variable will always have the value __main__ and if that python file is imported in any other python script then the
__name__ variable will have the name of the module.
Let's take an example to understand. Create a python script with name test.py an add the following code in it:
# defining a function def something(): print('Value of __name__:', __name__) something()
Value of __name__: __main__
When we executed the test.py script, the value for the
__name__ variable is set as __main__.
Now, let's create another python script main.py and then import the above script in it.
# importing test.py import test test.something()
Value of __name__: test
As you can see in the above code output, the value of the
__name__ variable is test, because we printed the value of the
__name__ variable of the test.py module.
if __name__ == __main__
To specify some code in any python script which should be executed only when that script is executed directly, we can use the
if statement with the condition
__name__ == __main__
# importing test.py import test if __name__ == __main__: test.something()
Here __main__ is just 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 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.
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.