Exiting Python Scripts

Sometimes you may want to exit a Python script early. An example of this is if an error has been encountered, and you can’t continue.

There are several ways to do this. Some commonly used methods are:

quit()
exit()

These are the same (exit is an alias for quit).

Even though they’re commonly used, they’re bad practice. In fact, if you’re using IDLE, you’ll get this ugly message:

So, what can we do that’s more graceful? Try this:

import sys
sys.exit()