See the Tutorial List

__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.

Also, __name__ variable is not available in Python 2.x version and was introduced in Python 3.0

What is __name__?

__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,

# defining a function
def something():
    print('This function is in Script1.')
if __name__=='__main__':
    print('Called from Script1.')
    print('Script1 is imported in some other file.')

When we run python then:

This function is in Script1. Called from Script1.

Now, let's create another python script file with name and import the script in it and try to call the function something() defined in the script Script1.

Here's the code for

import Script1

if __name__=='__main__':
    print('Called from Script2.')

Now we run python then:

Script1 is imported in some other file. This function is in Script1. Called from Script2.

When the 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

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

# import file
import Script1

print('Value or __name__ variable: ' + __name__)

Here is a live example:

NOTE: In the live example the scripts are and, the change in name of the scripts will not affect the functionality.