Where is main() function in a Python script?
- The following pattern protects the code from being run when a file containing it is
import-ed. - The global variable
__name__is set to"__main__"only when the script is run rather than being imported. - Don’t make
main()fiddle withsys.exit(). Instead, makemain()return the exit code — success (0) or failure (>0).
import sys
def main():
return 0
if __name__ == "__main__":
sys.exit(main())
— Oliver Frolovs, 2020