Sub topic 2
Leaving the Shell: exit(), quit(), and End-of-File Keys
This chapter assumes you can start the interactive shell with python or python3 and see the >>> prompt. You should also know what an exception is, because exiting the shell uses one.
The ways out
Python offers several ways to leave the shell. They all end the session, but they differ in how they work and where they run.
| Method | Works on | Notes |
|---|---|---|
| `exit()` | All platforms | Added by the `site` module. |
| `quit()` | All platforms | Same behavior as `exit()`. |
| Ctrl-D | macOS, Linux | Sends end-of-file (EOF). Must be on an empty line. |
| Ctrl-Z, then Enter | Windows | Sends end-of-file on the Windows console. |
| `sys.exit()` | All platforms | Needs `import sys`. Also works in scripts. |
exit() and quit()
exit and quit are not keywords or built-in functions in the strict sense. The site module creates them at startup and adds them to the built-in names. Both are objects of the same class, and calling either one closes standard input and raises SystemExit.
>>> exit()In a Python version older than 3.13, typing the bare name exit does not quit. It prints a hint:
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exitPython 3.13 and later accept the bare exit and quit in the interactive shell. Keep using the parentheses so your habit works on every version.
End-of-file keys
The shell reads lines from standard input. An end-of-file signal tells it that no more input will arrive, so it stops.
- On macOS and Linux, press Ctrl-D.
- On Windows, press Ctrl-Z and then Enter.
The key only counts on an empty prompt line. If you have typed part of a line, Ctrl-D does not exit.
Exit status
A process reports a number to the operating system when it ends. By default it is 0, which means success. You can pass a different value to exit().
>>> exit(3)Then check the status in your terminal:
echo $?This prints 3. On Windows Command Prompt, use echo %ERRORLEVEL%. If you pass a string, such as exit("config missing"), Python prints it to standard error and uses status 1.
Common errors
- Ctrl-C does not leave the shell. It raises
KeyboardInterrupt, which cancels the current line, the...block you are typing, or the running code, and then returns you to>>>. - Ctrl-D does nothing in the middle of a line. Clear the line first.
exit()can be missing when Python starts with the-Soption, which skips thesitemodule. Useimport sysandsys.exit()in that case.- Pressing Ctrl-Z on macOS or Linux does not exit Python. It suspends the process and sends it to the background. Type
fgto resume it.
Why this matters for the session
Leaving the shell discards every variable you defined. Anything you want to keep must go into a file first. Later chapters cover python -i, which runs a script and then keeps you at the prompt, and the workflow for moving code into files.
Exercise
You are in the shell and press Ctrl-D, but nothing happens. The prompt shows >>> total = 4. What is the cause, and what should you do?
Show answer
Ctrl-D only signals end-of-file on an empty line, and this line still has text on it. Clear the line (for example, delete it with Backspace or press Ctrl-C) and press Ctrl-D again, or type exit().
Exercise
You run exit(2) in the shell on Linux. What does echo $? print afterward, and why?
Show answer
It prints 2. exit(2) raises SystemExit with the code 2, and the interpreter passes that code to the operating system as its exit status.