Signup/Sign In

Python __name__ Variable

Python __name__ is a special variable in which the name of the current python script/module being executed is stored.

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

What is __name__?

__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 an add the following code in it:

# defining a function
def something():
    print('Value of __name__:', __name__)

Value of __name__: __main__

When we executed the script, the value for the __name__ variable is set as __main__.

Now, let's create another python script and then import the above script in it.

# importing
import test


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

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

For example,

# importing
import test

if __name__ == __main__:

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.

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

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.