What Does the Name-Main Idiom Do?
Let’s start by looking at what
if __name__ == "__main__" is actually doing. To demonstrate, here’s an example program that doesn’t use
if __name__ == "__main__" in it. In the file
repeat.py, you’ve got a
repeat() function that accepts some text and a number and returns that text repeated that number of times with newline characters in between so that they print on separate lines.
At the bottom of the file, it asks the user to input some text, and then it calls the
repeat() function with that text and the default number of repetitions, which is
2, and then it prints it out.
Here on the left, you can see our code for
repeat.py, and on the right we’ve got our console or terminal. To run this file as a script, we can use the
python command and then the script name, which is
I’m going to go back to the terminal and use the
python command to open up the Python console or Python REPL. We can do
from repeat, which is the file,
import repeat, which is the function. Okay, and it’s asking us for some text. That’s a bit odd.
Yep, that works. So we can still import that
repeat() function, but there’s a bit of a side effect happening here, which is that it’s asking us for some text, and that’s not what we expect when we’re just trying to import a function into our console.
02:48 This is happening because even if you’re just importing one function, the whole file is executed first, and then the definitions are imported in. Let’s look at another example where you’re importing into another file.
Here’s a file called
lyrics.py, and at the top, you’ve got an import statement that says
from repeat import repeat. Note that in order for this import statement to work as written,
lyrics.py must be in the same folder as
So after the first line that imports the
repeat() function from the
repeat module, it prints the lyrics for the song “Around the World” by Daft Punk, calling the
repeat() function with the text of
"Around the world" and
144 repetitions, and then it prints it out. So in the terminal, if we ran
python lyrics.py, it’s now asking us for some text.
03:50 Once we press Enter, the rest of the file runs as expected. So that’s not what we want. When we just look at this file, it doesn’t look like we should be having to type anything in first before the file runs. So how do we fix this?
Let’s try adding
if __name__ == "__main__" to
repeat.py. Here, we’re back in the
repeat.py file, and just after the
repeat() function, we are going to add
if __name__ == "__main__" as a string and then indent the next two lines and save it.
it worked. So what is
if __name__ == "__main__" actually doing? Well, it allows you to specify some code that only executes when the file is run as a script, and it doesn’t execute when the file is imported as a module.
05:38 Now there’s no official name for this line of code, but as stated in the overview, you can call it the name-main idiom. And if you look at this page in the Python documentation, you can see why.
Here’s the documentation where this line of code is referenced, and it’s talking about the
"__main__" name. And if you scroll down to Idiomatic Usage, you can actually see where
if __name__ == "__main__" is being referenced in the documentation.
Now that we know what the name-main idiom does, why is it useful? Well, for one, it allows you to avoid side effects when importing files as modules, like we saw when
repeat.py was asking for some text input.
And this is the case when you’re importing into the Python REPL or into another file. Or if you already have a module, it allows you to add an entry point to that file so that you can use a function in it, like
repeat(), right from the command line.
Become a Member to join the conversation.