all Python 3Built-in typesterm
exit
special values
An object the site module adds for the interactive shell: exit() raises SystemExit. It is not a built-in function proper; in programs use sys.exit().
Syntax
exit(code=None)
The names exit and quit are put into the built-in namespace by the site module at startup; run python -S and they are gone, so a script calling exit() dies with NameError. It is an instance of the Quitter class: calling it closes sys.stdin and raises SystemExit with the given code, while print(exit) only prints a hint. The docs say outright not to use it in programs — use sys.exit(), which is always there; since 3.13 the new REPL also accepts exit without parentheses.
Examples
import sys
print(type(exit).__name__, type(exit).__module__) # → Quitter _sitebuiltins
try:
exit(3) # → SystemExit
except SystemExit as e:
print(e.code) # → 3
print(sys.stdin.closed) # → True — exit() закрыл stdin
import subprocess
import sys
code = 'import builtins; print(hasattr(builtins, "exit"))'
r = subprocess.run([sys.executable, '-S', '-c', code],
capture_output=True, text=True)
print(r.stdout.strip()) # → False — без site имени exit нет
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy