Is there a way to prevent a SystemExit exception raised from sys.exit() from being caught?

You can call os._exit() to directly exit, without throwing an exception:

import os
os._exit(1)

This bypasses all of the python shutdown logic, such as the atexit module, and will not run through the exception handling logic that you’re trying to avoid in this situation. The argument is the exit code that will be returned by the process.

Leave a Comment