🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

__name__ == '__main__'

AI & DATA SCIENCE // name-main

The __name__ == '__main__' check lets a Python file detect whether it was run directly, versus imported as a module by another file.

Syntax

if __name__ == "__main__":
    main()

Deep Dive Course

Every module has a built-in __name__ attribute: when a file is run directly, Python sets that file's __name__ to the string '__main__'; when the same file is instead imported by another module, __name__ is set to the module's actual name instead. Wrapping code in this check means that code only runs when the file is executed directly, not when it's imported elsewhere purely to reuse its functions or classes, letting the same file serve as both a reusable module and a standalone script.

1Understanding __name__ == '__main__'

Every module has a built-in __name__ attribute: when a file is run directly, Python sets that file's __name__ to the string '__main__'; when the same file is instead imported by another module, __name__ is set to the module's actual name instead. Wrapping code in this check means that code only runs when the file is executed directly, not when it's imported elsewhere purely to reuse its functions or classes, letting the same file serve as both a reusable module and a standalone script.

💡

Put any code meant only for 'running this file directly' — a demo, a command-line entry point, test invocations — inside the __name__ == '__main__' guard, so importing the file elsewhere doesn't accidentally trigger side effects like printing output or launching a server.

editor.html
def greet():
    print("Hello from the script")

if __name__ == "__main__":
    greet()
localhost:3000

2Practical Example

Here is a real-world application of __name__ == '__main__' showing how it is used in production Python code.

editor.html
# math_utils.py
def square(x):
    return x * x

if __name__ == "__main__":
    print("Running self-test...")
    assert square(4) == 16
    print("All tests passed")

# Importing this file elsewhere would NOT print anything,
# since __name__ would be 'math_utils', not '__main__'.
localhost:3000

3Best Practices

Follow these guidelines when working with __name__ == '__main__':

1. Put a script's top-level, side-effect-causing logic inside the __name__ == '__main__' guard instead of directly at module level

2. Define the file's actual reusable logic in properly named functions, and call the entry-point function from inside the guard

3. Keep imports and function/class definitions outside the guard, since those should still work when the file is imported as a module

⚠️

Tip: Put any code meant only for 'running this file directly' — a demo, a command-line entry point, test invocations — inside the __name__ == '__main__' guard, so importing the file elsewhere doesn't accidentally trigger side effects like printing output or launching a server.

editor.html
def greet():
    print("Hello from the script")

if __name__ == "__main__":
    greet()
localhost:3000

Examples

Example 01Basic Usage
def greet():
    print("Hello from the script")

if __name__ == "__main__":
    greet()
Example 02Advanced Example
# math_utils.py
def square(x):
    return x * x

if __name__ == "__main__":
    print("Running self-test...")
    assert square(4) == 16
    print("All tests passed")

# Importing this file elsewhere would NOT print anything,
# since __name__ would be 'math_utils', not '__main__'.

Best Practices

  • Put a script's top-level, side-effect-causing logic inside the __name__ == '__main__' guard instead of directly at module level
  • Define the file's actual reusable logic in properly named functions, and call the entry-point function from inside the guard
  • Keep imports and function/class definitions outside the guard, since those should still work when the file is imported as a module

Interview Question

If a file named utils.py is imported by another script, what does __name__ equal inside utils.py at that point?

Hint: Think about the two different ways __name__ can be set.

Inside utils.py, __name__ would equal the string 'utils', the module's own name, not '__main__'. Python only sets a file's __name__ to '__main__' when that specific file is the one being executed directly as the program's entry point; any file loaded via an import statement gets its own module name instead, which is exactly the distinction the __name__ == '__main__' guard relies on.

Exercises

MediumPractice using __name__ == '__main__' in a real scenario.
View Solution
def greet():
    print("Hello from the script")

if __name__ == "__main__":
    greet()

Frequently Asked Questions

If a file named utils.py is imported by another script, what does __name__ equal inside utils.py at that point?

Inside utils.py, __name__ would equal the string 'utils', the module's own name, not '__main__'. Python only sets a file's __name__ to '__main__' when that specific file is the one being executed directly as the program's entry point; any file loaded via an import statement gets its own module name instead, which is exactly the distinction the __name__ == '__main__' guard relies on.

Related Functions

import-statementdef-keywordas-keyword