0%

🪄 Scripts Have Souls Too

When you run it directly, name becomes “main”. When it’s imported, it takes the name of its module. That’s how Python knows who’s performing and who’s just part of the spell.
🔮 print(name)

Every script has an identity. A name it answers to. When you call it directly, it knows it is the main performer. When another script calls it, it knows it is just a supporting voice. The __name__ variable holds this identity. Python sets it automatically before your code runs. You never touch it. You just read it. And based on what it says, your script decides how to behave.

🕯️ Magic Note

When a Python file is executed directly, Python sets __name__ to “__main__”. When the same file is imported by another module, Python sets __name__ to the module’s name (the filename without the .py extension). This simple mechanism lets you write code that behaves differently in each situation.

The syntax print(__name__) reveals the current identity. In your main script, it prints “__main__”. In an imported module, it prints the module name like “my_module”. This is how Python distinguishes the performer from the ensemble.
  • __name__ is set automatically by Python before execution
  • Direct execution sets __name__ to “__main__”
  • Import execution sets __name__ to the module’s name
  • Use if __name__ == “__main__”: to guard code that should only run on direct execution
💡 Always use the if __name__ == “__main__”: guard for test code or example usage in reusable modules. This allows your file to be both an importable library and a standalone script. Without this guard, test code would run every time someone imports your module, which is rarely what you want.
Execution Method__name__ Value
Direct run: python script.py“__main__”
Import: import script“script”
Import from package: from package import module“package.module”
Interactive shell (REPL)“__main__”
⚠️ The __name__ variable is read only. Do not try to assign a value to it. Also remember that the “__main__” check works only in the file where it is written. If you import a module that has this guard, the guarded code will not run on import, only when that module is executed directly.
Examples

Python

# Guard pattern for direct execution

def main():

print(“This is the main function”)

print(“Running the spell directly”)

if __name__ == “__main__”:

main()

# Output when run directly: This is the main function

# Output when run directly: Running the spell directly

# Output when imported: (nothing runs)

Python

# Printing __name__ in a module (save as spell.py)

# File: spell.py

print(f”__name__ is: {__name__}”)

# When run directly: python spell.py

# Output: __name__ is: __main__

# When imported: import spell

# Output: __name__ is: spell

Python

# Module with both reusable functions and test code

def add(a, b):

return a + b

def multiply(a, b):

return a * b

if __name__ == “__main__”:

print(“Testing the module:”)

print(f”add(3, 4) = {add(3, 4)}”)

print(f”multiply(3, 4) = {multiply(3, 4)}”)

# Output when run directly: Testing the module: add(3, 4) = 7 multiply(3, 4) = 12

# Output when imported: nothing prints, but add() and multiply() are available

Common Mistakes
  • Forgetting the double underscores on both sides of __name__ and __main__
  • Writing if __name__ = “__main__”: with single equals instead of ==
  • Assuming the guard works inside functions or classes, it checks the module name, not the function context

⚡ Whisper

Every script carries a name. Called directly, it speaks its own truth. Imported, it whispers another. The name tells the role. Watch closely, and you will know who is performing and who is just part of the spell.