Skip to content

Latest commit

 

History

History
17 lines (12 loc) · 519 Bytes

File metadata and controls

17 lines (12 loc) · 519 Bytes

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 with sys.exit(). Instead, make main() return the exit code — success (0) or failure (>0).
import sys

def main():
  return 0
  
if __name__ == "__main__":
  sys.exit(main())

— Oliver Frolovs, 2020