Remember
Python does not require a main() function. The __name__ guard lets a file run its entry point when executed directly, without doing so on import.
Python does not require a main() function. The __name__ guard lets a file run its entry point when executed directly, without doing so on import.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
def main():
print('Program started')
if __name__ == "__main__":
main()Top-level scope
function mainPython does not require a main() function. The __name__ guard lets a file run its entry point when executed directly, without doing so on import.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
def main():
print('Program started')
if __name__ == "__main__":
main()
|
v
DISPLAYED RESULT
Program started
FINAL BINDINGS
main --> object 1 (function)
object 1: function mainChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
def main():
print('Program started')
if __name__ == "__main__":
main()Top-level scope
function mainThe example has finished. Compare the final output with the values and references above.
Program started
What do you expect this program to print?
def main():
print('Program started')
if __name__ == "__main__":
main()Unlike many other programming languages, Python doesn't require a main function to start execution. That might sound liberating at first—but it also means structure is your responsibility.
This tutorial will help you understand how and why to use a main() function in Python. Whether you’re writing a script or building a module, learning to organize your code using main() and __name__ == "__main__" is a fundamental skill.
Python executes code from top to bottom. If you don’t take care, everything in your script might run immediately when imported elsewhere. To avoid this, we use a main guard pattern:
def main():
print("Hello from main function!")
if __name__ == "__main__":
main()
Python assigns a special variable called __name__ to every module. When a script is run directly, __name__ is set to "__main__". But if the same script is imported, __name__ becomes the module's name.
# file: greet.py
print("This is always printed")
def hello():
print("Hello!")
if __name__ == "__main__":
print("This runs only when executed directly")
import greet
This is always printed
Notice how "This runs only when executed directly" did NOT appear? That’s the power of __name__ == "__main__"—it guards what should only run in standalone mode.
Create a main() function that wraps the core logic of your script.
def main():
print("Starting the program")
# Add your core logic here
Right at the bottom of your file, add the guard to control script behavior:
if __name__ == "__main__":
main()
Save your script as my_script.py and run it using:
python my_script.py
Starting the program
main().__name__ == "__main__", your code may run unexpectedly during import.main() before defining it or outside the guard can lead to issues.main.
For more advanced Python scripts, especially command-line utilities, you can accept input parameters by using the sys.argv list from the sys module.
import sys
def main():
print("Arguments passed:", sys.argv)
if __name__ == "__main__":
main()
sys.argv is a list that contains the command-line arguments passed to the script.sys.argv[0], is always the name of the script.If you run this script from the command line like this:
python my_script.py arg1 arg2
The output would be:
Arguments passed: ['my_script.py', 'arg1', 'arg2']
This approach allows you to build flexible, CLI-based Python utilities that accept dynamic input from users, such as filenames, configuration flags, or processing options.
While Python doesn’t enforce a main() function like C or Java, learning to use it adds clarity, safety, and professionalism to your scripts. It’s one of those “small things” that sets experienced Python developers apart.
Always ask yourself: Could this code be imported in the future? If yes, then guard it well.
Connect what you noticed in the example with the idea below.
Python does not require a main() function. The __name__ guard lets a file run its entry point when executed directly, without doing so on import.
Python automatically calls every function named main.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.