Explaining ‘exit(1)’ in Python

    try:
env = os.environ["ENV"]
tag["env"] = env
except KeyError as e:
logger.error("Required ENV not found")
exit(1)
  • when a UNIX/bash program ends, it returns an 8-bit code 0 and 255
  • bash does not support exceptions (PowerShell does) therefore the use of exit(0/1) in Python is idiomatic in the context of infrastructure as code and a combination of Python/Bash scripts
  • conventionally, 0 is a success, 1-255 are errors and it is essential to follow conventions here
  • exit(0) means a clean exit without any errors / problems -> A zero error code means a successful exit.
  • exit(1) means there was some issue/error / problem and that is why the program is exiting; any non-zero exit code is treated as an abnormal exit
  • in addition, this is not Python-specific and is conventional
  • in UNIX/Linux utilities the error code indicates what the problem was.

LINKS

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

63 Followers

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com