4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to exit Python in terminal

By 4Geeks

Leave the Python prompt with exit(), quit() or a keyboard shortcut, stop running scripts with Ctrl + C and end programs with sys.exit().

6 min readLast updated
Exit Python in the terminal icon

How do you exit Python in the terminal?

Short answer: type exit() or quit() and press Enter. You can also use the keyboard: Ctrl + D on macOS and Linux, or Ctrl + Z and then Enter on Windows. Since Python 3.13 you can type just exit or quit, without parentheses.

>>> exit()
$

If a script is running and you want to stop it, press Ctrl + C. Inside your own code, use sys.exit().

exit() closes the interactive Python, Ctrl + C stops a script that is running and sys.exit() ends your program from the code.

How to exit Python in every situation

Where you areHow to exit
Python >>> on macOS or Linuxexit(), quit() or Ctrl + D
Python >>> on Windowsexit(), quit() or Ctrl + Z and then Enter
Python 3.13 or newerAlso exit or quit without parentheses
A script that is runningCtrl + C
Inside your own codesys.exit()
The help> promptType quit or press Enter on an empty line
The ... promptPress Enter on an empty line, or Ctrl + C to cancel
IPythonexit, quit or Ctrl + D
Jupyter NotebookThere is no exit: use Kernel > Restart or Shut Down

Exit with exit() or quit()

python
>>> exit()

exit() and quit() do exactly the same thing: they close the interactive interpreter and take you back to your system prompt ($ on macOS and Linux, C:\Users\you> on Windows).

In Python 3.12 or older, typing exit without parentheses does not close Python. It only prints a reminder:

>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit

On Windows the message says Use exit() or Ctrl-Z plus Return to exit. Add the parentheses and Python closes. Since Python 3.13, the new interactive shell understands exit and quit on their own.

Exit with the keyboard

SystemShortcut
macOS and LinuxCtrl + D at an empty >>> prompt
Windows (Command Prompt or PowerShell)Ctrl + Z, then Enter

On macOS and Linux, Ctrl + Z does not close Python. It pauses it in the background and the shell shows something like zsh: suspended python3. Type fg to bring Python back and then exit with exit(), or end it from the shell with kill %1.

Career · AI Engineering

You know your way around Python, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Stop a running script with Ctrl + C

If your program is stuck in a loop or taking too long, press Ctrl + C in the terminal where it runs. Python raises KeyboardInterrupt and stops:

$ python3 -c "while True: pass"
^CTraceback (most recent call last):
  File "<string>", line 1, in <module>
KeyboardInterrupt

That traceback is normal: it only says you stopped the program. If Ctrl + C does not work because the code catches every exception, close the terminal tab or end the process from your system (kill followed by the process number on macOS and Linux, or the Task Manager on Windows).

Exit from your code with sys.exit()

exit() and quit() are meant for the interactive shell. Inside a script, use sys.exit():

python
import sys
 
file_name = "data.csv"
 
if not file_name.endswith(".csv"):
    print("Error: I need a CSV file")
    sys.exit(1)
 
print("Processing", file_name)

The number is the exit code: 0 means everything went well and any other number means there was an error. Other programs and CI pipelines read it, and you can check it in the terminal with echo $? on macOS and Linux:

$ python3 -c "import sys; sys.exit(1)"
$ echo $?
1

If you pass text instead of a number, Python prints it and uses the exit code 1:

python
raise SystemExit("Error: the file is missing")

exit(), quit(), sys.exit() and os._exit()

FunctionWhere to use itWhat it does
exit() / quit()Interactive shellClose the >>> prompt
sys.exit()Scripts and programsRaises SystemExit with an exit code; finally blocks still run
os._exit()Very special cases, such as child processesEnds the process at once, without cleanup or flushing files

Exit Python in VS Code, Jupyter and IPython

  • VS Code: the integrated terminal works like any other terminal: exit(), Ctrl + D or Ctrl + Z and Enter. To stop a script you started with the Run button, press Ctrl + C in the terminal or click the trash icon to close it.
  • Jupyter Notebook and JupyterLab: the kernel keeps running until you restart it. Use Kernel > Interrupt to stop a cell, and Restart or Shut Down to end Python.
  • IPython: type exit, quit or press Ctrl + D.

Use AI when Python will not let you go

An assistant like ChatGPT, Claude or GitHub Copilot can tell you exactly what is happening in your terminal if you paste what you see. These three prompts are ready to copy.

1. Python does not close

I am in the Python terminal on [Windows / macOS / Linux] and I cannot exit.
This is what I see: [paste the last lines of your terminal]
Tell me how to exit and why what I tried did not work.

2. Stop a program safely

My Python script [describe what it does] sometimes gets stuck.
Show me how to stop it with Ctrl + C without losing data, using try/except KeyboardInterrupt and finally.

3. Use exit codes

Write a Python script that validates [what you need to check] and ends with sys.exit(0) if it is OK
or sys.exit(1) if it fails, so I can use it in [a CI pipeline / a bash script].

Check every answer by running it in your own terminal.

Learn with a mentor

From the terminal to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when exiting Python

What you seeWhy it happensFix
Use exit() or Ctrl-D (i.e. EOF) to exitYou typed exit without parentheses in Python 3.12 or olderType exit()
zsh: suspended python3You pressed Ctrl + Z on macOS or Linux, which pauses PythonType fg and then exit(), or kill %1
A KeyboardInterrupt tracebackYou pressed Ctrl + C to stop a scriptIt is normal; catch it with try/except KeyboardInterrupt if you want a clean message
The ... prompt does not go awayPython is waiting for the rest of a blockPress Enter on an empty line, or Ctrl + C
NameError: name 'sys' is not definedYou called sys.exit() without importing sysAdd import sys at the top
The script keeps running after sys.exit()A bare except: caught the SystemExitUse except Exception: so SystemExit can pass
Free download

Exiting Python in a 2-page cheat sheet

Every way to leave Python on Windows, macOS and Linux, Ctrl + C, sys.exit() and exit codes, VS Code and Jupyter, common problems and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to exit Python cheat sheet

2 pages · PDF · 4Geeks

  • The exact command or shortcut for every situation
  • sys.exit() and exit codes for your scripts
  • Common problems and prompts to fix them with AI

Get it free

Your PDF right away

Frequently asked questions about exiting Python

Want to go from running Python in the terminal to building AI apps, with real projects and mentors?

See the AI Engineering program