Python __name__

Summary: in this tutorial, you’ll learn about the Python __name__ variable and how to use it effectively in modules.

What’s Python __name__?

If you have gone through Python code, you’ve likely seen the __name__ variable like the following:

if __name__ == '__main__':
    main()Code language: Python (python)

And you might wonder what the __name__ variable is.

Since the __name__ variable has double underscores at both sides, it’s called dunder name. The dunder stands for double underscores

The __name__ is a special variable in Python. It’s special because Python assigns a different value to it depending on how its containing script executes.

When you import a module, Python executes the file associated with the module.

Often, you want to write a script that can be executed directly or imported as a module. The __name__ variable allows you to do that.

When you run the script directly, Python sets the __name__ variable to '__main__'.

However, if you import a file as a module, Python sets the module name to the __name__ variable.

Python __name__ variable example

First, create a new module called billing that has two functions: calculate_tax() and print_billing_doc(). In addition, add a statement that prints out the __name__ variable to the screen:

def calculate_tax(price, tax):
    return price * tax

def print_billing_doc():
    tax_rate = 0.1
    products = [{'name': 'Book',  'price': 30},
                {'name': 'Pen', 'price': 5}]

    # print billing header

    # print the billing item
    for product in products:
        tax = calculate_tax(product['price'], tax_rate)

print(__name__)Code language: Python (python)

Second, create a new file called and import the billing module:

import billingCode language: Python (python)

When you execute the

> python app.pyCode language: CSS (css)

…the __name__ variable shows the following value:

billingCode language: Python (python)

It means that Python does execute the file when you import the billing module to the file.

The __name__ variable in the set to the module name which is billing.

If you execute the as a script directly:

> python billing.pyCode language: CSS (css)

… you’ll see the following output:

__main__Code language: Python (python)

In this case the value of the __name__ variable is '__main__' inside the

Python __name__

Therefore, the __name__ variable allows you to check when the file is executed directly or imported as a module.

For example, to execute the print_billing_doc() function when the executes directly as a script, you can add the following statement to the module:

if __name__ == '__main__':
    print_billing_doc()Code language: Python (python)

Third, execute the as a script, you’ll see the following output:

Name    Price   Tax
Book    30      3.0
Pen     5       0.5Code language: Python (python)

However, when you execute the, you won’t see the if block executed because the __name__ variable doesn’t set to the '__main__' but 'billing'.


  • Python assign the '__main__' to the __name__ variable when you run the script directly and the module name if you import the script as a module.
Did you find this tutorial helpful ?