MemotivaModules, packages, and imports

What is the if name equals main idiom?

Modules, packages, and imports

Audio flashcard · 0:16

Nortren·

What is the if name equals main idiom?

0:16

When a Python file is run directly, its dunder name attribute is the string main. When imported as a module, dunder name is the module name. The idiom checks for main to run code only when the file is executed directly, not when it is imported. It is essential for files that are both libraries and scripts.
docs.python.org