Lesson 44 of 60
โฎPrev Nextโฏ

PY-101 ยท Modules and packages

if __name__ == "__main__"

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

44of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    Every Python file has a built-in variable __name__. It is "__main__" when you run the file directly, and the module's name when another file imports it. Putting start-up code under if __name__ == "__main__": means it runs only when the file is run, not when it is imported.

    • Know what __name__ holds
    • Guard start-up code
    • Make a file both a module and a script

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    if __name__ == "__main__", running
    1. def greet(name):
    2. return f"Hello, {name}!"
    3. def main():
    4. print(greet("Asha"))
    5. print("Running as:", __name__)
    6. if __name__ == "__main__":
    7. main()
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou write useful functions in tools.py with a quick demo at the bottom. A colleague imports tools.py and the demo suddenly runs inside their program. The guard stops that.
    2. The Python ideadef main():Every Python file has a built-in variable __name__.
    3. In real lifeCommand-line tools put argument parsing in main(), so their functions can also be imported and tested without running the tool.

    Start with a question

    You write useful functions in tools.py with a quick demo at the bottom. A colleague imports tools.py and the demo suddenly runs inside their program. The guard stops that.

    Every reusable Python script uses this pattern. You will see it at the bottom of almost every file.

    Explain it like I'm 10

    It is a note on the door: "only do the welcome show if you came in through the front door, not if someone carried you in".

    The technical explanation

    Run python tools.py and inside it __name__ == "__main__".

    import tools from another file sets tools.__name__ to "tools", so the guarded block is skipped.

    In this editor your code runs as the main program, so the guarded block runs.

    Syntax

    def main():
        ...
    
    if __name__ == "__main__":
        main()

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Command-line tools put argument parsing in main(), so their functions can also be imported and tested without running the tool.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is __name__ when you run a file directly?
    2. 2. What is __name__ when the file is imported as tools?
    3. 3. Why use the guard?
    4. 4. How many underscores surround name and main?
    5. 5. Best practice inside the guard?

    Questions people ask

    Is if __name__ == "__main__" required?

    No. A script works without it. It matters when the file might be imported, which is most files that grow beyond a few lines.

    What does dunder mean in Python?

    "Double underscore", the names like __name__ and __init__ that Python uses for special meaning.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Is if __name__ == "__main__" required?

    No. A script works without it. It matters when the file might be imported, which is most files that grow beyond a few lines.

    beginner What does dunder mean in Python?

    "Double underscore", the names like __name__ and __init__ that Python uses for special meaning.

    intermediate What is __name__ when you run a file directly?

    "__main__". Directly run files are __main__.

    intermediate What is __name__ when the file is imported as tools?

    "tools". It is the module name.

    Quick revision

    Remember

    • Know what __name__ holds
    • Guard start-up code
    • Make a file both a module and a script
    • Put the guarded code in a main() function and call main() under the guard. It keeps the global scope clean and makes main testable.

    Syntax

    def main():
        ...
    
    if __name__ == "__main__":
        main()

    Official documentation