In Python, there are two similarly-named functions, exit() and sys.exit(). What’s the difference and when should I use one over the other?
In Python, there are two similarly-named functions, exit() and sys.exit() . What’s the difference
Share
exitis a helper for the interactive shell –sys.exitis intended for use in programs.Technically, they do mostly the same: raising
SystemExit.sys.exitdoes so in sysmodule.c:While
exitis defined in site.py and _sitebuiltins.py, respectively.Note that there is a third exit option, namely os._exit, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a
fork()).