Why doesn’t the main() function run when I start a Python script? Where does the script start running?

You’ve not called your main function at all, so the Python interpreter won’t call it for you.

Add this as the last line to just have it called at all times:

main()

If you use the commonly seen:

if __name__ == "__main__":
    main()

It will make sure your main method is called only if that module is executed as the starting code by the Python interpreted, more about that is discussed here: What does if __name__ == “__main__”: do?

If you want to know how to write the best possible ‘main’ function, Guido van Rossum (the creator of Python) wrote about it here.

Leave a Comment