What does `if __name__ == "__main__":` do?

James W.

What does if __name__ == "__main__": do?

The Problem

You may have come across this if __name__ == "__main__": code block and wondered what it is used for and whether you can use it in your own code.

What does this line of logic do? What are these variables referring to?

The Solution

To understand what if __name__ == "__main__": does, let’s take a look at this variable: __name__.

The __name__ variable is set by our Python interpreter when we run our code and it’s defined for each file.

If the file is used as the starting point of your program, then the __name__ variable is set to "__main__". If the file is imported, then the __name__ variable is set to the filename without the .py extension. For example, if you import some_calc.py, then __name__ will be some_calc.

We can use this __name__ variable to allow a file to execute different code blocks based on whether that file is being run as the main program or as an imported module.

Example

In a file called file_one.py below, three functions are defined and called:

# file_one def always(): print("Always") def on_import(): print("On import") def when_main(): print("When Main") always() if __name__ == "__main__": when_main() else: on_import()

The output of the code above, when run as the main program, is this:

Always When Main

The always() function is executed whether the file is run as the main program or imported as a module. The when_main() function, however, is executed because this file is executed as the main program.

In another scenario, a different file imports the file above, like so:

# file_two import file_one if __name__ == "__main__": print("file_two is the main program")

If this file_two.py is run as the main program, the output will be:

Always On import file_two is the main program

Here the on_import() function is executed in file_one. This is because the else statement is triggered in the if __name__ == "__main__": code block, as file_one is not the main program but is executed on import.

The always() function from file_one is also executed, as it falls outside of the if-else statement and is executed whether file_one is the main program or imported.

Finally, file_two triggers the if statement of the if __name__ == "__main__": code block and prints.

Get Started With Sentry

Get actionable, code-level insights to resolve Python performance bottlenecks and errors.

  1. Create a free Sentry account

  2. Create a Python project and note your DSN

  3. Grab the Sentry Python SDK

pip install --upgrade sentry-sdk
  1. Configure your DSN
import sentry_sdk sentry_sdk.init( "https://<key>@sentry.io/<project>", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production. traces_sample_rate=1.0, )

Check our documentation for the latest instructions.

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.