James W.
—if __name__ == "__main__":
do?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?
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.
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.