4

According to Difference between exit() and sys.exit() in Python, the preferred way to exit from a Python application is to use sys.exit(). Python's documentation states that exit() should only be used to exit from the interactive shell.

The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.

While the documentation warns against using constants from the site module, it doesn't explain why we shouldn't use them.

Why shouldn't exit() be used in Python applications?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

1 Answers1

2

The site module, which is responsible for injecting exit() and other things into global scope for convenience in the REPL, is not guaranteed to be loaded. If you rely on it, and it's not there, your app will break unexpectedly.

Just to be safe, from sys import exit will do you no harm.

Taku
  • 31,927
  • 11
  • 74
  • 85
Adam Barnes
  • 2,922
  • 21
  • 27