Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

What Does if __name__ == "__main__" Mean in Python? (Summary)

You’ve learned what the if __name__ == "__main__" idiom does in Python. It allows you to write code that executes when you run the file as a script, but not when you import it as a module. It’s best to use it when you want to collect user input during a script run and avoid side effects when importing your module—for example, to unit test its functions.

You also got to know some common but suboptimal use cases and learned about better and more idiomatic approaches that you can take in those scenarios. Maybe you’ve accepted Python’s name-main idiom after learning more about it, but if you still dislike it, then it’s good to know that you can probably replace its use in most cases.

For further investigation, check out:

You can visit the following resources to lean more about testing in Python:

These resources will help you document your projects with Python:

Maybe you’d like to learn more about using the command line. Real Python’s got you covered:

When do you use the name-main idiom in your Python code? While working your way through this course, did you discover a way to replace it, or is there a good use case that we missed? Share your thoughts in the comments below.

Download

Sample Code (.zip)

3.2 KB
Download

Course Slides (.pdf)

12.3 MB

00:00 And you made it to the end, congratulations! To summarize what you learned throughout the course, the if __name__ == "__main__", we can call it the name-main idiom.

00:11 It checks to see if a file was run directly as a script or imported as a module, and it allows you to run some code under one condition and not the other.

00:22 To do that, it uses a regular if statement and makes use of Python’s global __name__ variable that stores the name of a module, which is going to be "__main__" if it’s in the top-level code environment.

00:35 It’s not an official entry point like main() functions in other languages. It’s often used to create command-line entry points for modules, and if you’re using it for another reason, consider some other alternatives first, especially if your program is larger or more complex.

00:55 As promised, here are some additional resources to help expand on the topics in this course.

01:02 The first tutorial will help you better understand how to run Python scripts, depending on your environment, platform needs, and skills as a programmer.

01:11 The second tutorial looks deeper at Python’s import system, looking at modules and packages. It touches on many topics like the __name__ variable and where modules can be imported from.

01:24 The last tutorial on this page covers a lot of the same material as this course, but also goes more in depth into the main() function specifically. I

01:35 touched on testing a couple of times throughout the course, and here’s a great introductory tutorial that can teach you how to create Python unit tests, execute them, and use them to find bugs.

01:46 The written article provides examples in the unittest framework that you saw in earlier lessons, while the video course provides examples using an excellent external framework called pytest. Next, as an alternative to providing demonstrations in a name-main idiom block, you can use docstrings to better communicate and document your code, especially for larger projects.

02:12 The first tutorial is a complete guide to documenting Python code. Whether you’re documenting a small script or a large project, whether you’re a beginner or seasoned Pythonista, this guide covers everything you need to know, including best practices for writing docstrings. In the second tutorial, you learn how to use the doctest module from the standard library to run automated tests from those docstrings.

02:38 And for the final set of resources, here are two tutorials to help you get a better grasp on creating command-line entry points. The first one teaches you all about Python command-line arguments and how they can be used to convert your programs into useful tools accessible from the command line.

02:56 The next tutorial shows you how to take your command-line Python scripts to the next level by adding a convenient command-line interface. This tutorial shows you examples using the argparse module found in the Python standard library.

03:10 There are also many other excellent external packages you can use to create command-line interfaces, which can be found in many of the other project-based Real Python tutorials.

03:23 Congratulations again on completing the course. I hope it has inspired you to learn more about the intricacies of Python. Thank you for watching and see you next time at realpython.com.

Mike Lesselyoung on Sept. 3, 2023

Sorry, but why you pronounce the “s” in re”s”ources as if it were a “z”? It is not a z, it is an s.

Bartosz Zaczyński RP Team on Sept. 4, 2023

@Mike Lesselyoung When you try searching “resources pronunciation” on Google, you’ll find that the British pronunciation of “s” in this term sounds closer to a “z.” Arianne is from Canada where some words mirror the British pronunciation.

Ramu Mohan Srinivasan Radhakrishnan on Sept. 10, 2023

Namasthe Arianne Dee madam, I had a tough time understanding if __name__ == '__main__' . We have a load of production code that has this line. Your video has cleared the air surrounding this. Thank you madam! I also want to thank the RP team for the great lineup of Python content available on the website. Madam, can you also include some more videos under this title indicating why .pyc files are generated when using a .py file as module ? Please review following and assist!

lsr /media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does
/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does:
total 28K
392606 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 15:06 00-what-does-if-name-eq-main-mean-in-python/
392607 drwxrwxr-x 3 ramumsrk ramumsrk ? 4.0K Sep 10 15:44 01-what-does-the-name-main-idiom-do/
392611 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 15:51 02-how-does-the-name-main-idiom-work/
392612 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 16:00 03-when-should-you-use-the-name-main-idiom/
392613 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 16:00 04-when-should-you-not-use-the-name-main-idiom/
392614 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 16:01 05-how-do-you-use-the-name-main-idiom/
392615 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 16:02 06-what-does-if-name-eq-main-mean-in-python-summary/

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/00-what-does-if-name-eq-main-mean-in-python:
total 0

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/01-what-does-the-name-main-idiom-do:
total 8.0K
392609 drwxrwxr-x 2 ramumsrk ramumsrk ? 4.0K Sep 10 15:44 __pycache__/
392608 -rw-rw-r-- 1 ramumsrk ramumsrk ?  176 Sep 10 15:13 repetitions.py

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/01-what-does-the-name-main-idiom-do/__pycache__:
total 4.0K
392610 -rw-rw-r-- 1 ramumsrk ramumsrk ? 622 Sep 10 15:44 repetitions.cpython-311.pyc

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/02-how-does-the-name-main-idiom-work:
total 0

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/03-when-should-you-use-the-name-main-idiom:
total 0

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/04-when-should-you-not-use-the-name-main-idiom:
total 0

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/05-how-do-you-use-the-name-main-idiom:
total 0

/media/ramumsrk/16GiB/realpython.com.git/lessons/what-if-name-main-python-does/06-what-does-if-name-eq-main-mean-in-python-summary:
total 0

Regards, Ramu Mohan Srinivasan Radhakrishnan

Bartosz Zaczyński RP Team on Sept. 11, 2023

@Ramu Mohan Srinivasan Radhakrishnan You can safely ignore the .pyc files, which stand for “compiled Python.” Whenever you import a Python module, the interpreter compiles your human-readable code into binary bytecode to speed up importing the same module next time. It won’t make your code run any faster, though. It’s just to make importing the module quicker in the future.

As to the other part of your comment, I’m not exactly sure what it is that you’re trying to achieve.

Ramu Mohan Srinivasan Radhakrishnan on Sept. 13, 2023

Hi Bartosz Zaczyński, I am listing my directory contents where the compiled python code is present. Thanks a lot for your help and assistance Bartosz

Become a Member to join the conversation.