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

How to exit Python in terminal

Exiting Python from the terminal can be achieved in 3 different ways. The exit and quit commands will exit Python and you can as well exit by using keyboard shortcuts such as ctrl+z and ctrl+d on Windows and Unix Based Systems respectively
4 min readLast updated

Short answer: type exit() or quit() and press Enter. You can also use the keyboard: Ctrl + D on macOS and Linux, or Ctrl + Z followed by Enter on Windows (Python documentation on the interpreter).

In Python, there are a few ways to exit the terminal. We are going to show you the exit() method because it doesn't prompt for confirmation and the method name is self-explanatory.

Type the following on your terminal:

python
exit()

Other ways to exit the python terminal

But there are other ways to achieve this:

Exit()

The exit() command terminates Python in a safe way. It's important to write the () after the word, or Python could understand that you want to know what exit is.

python
Use exit() or Ctrl-Z plus Return to exit

This is the reason why we're emphasizing the presence of the () at the end.

The syntax is quite straight-forward:

python
exit()

Using quit()

The command quit() follows the same syntax as exit(). You'll need to add the () at the end or you'll receive the same message as before.

python
Use quit() or Ctrl-Z plus Return to exit

Using shortcuts

Keyboard shortcuts are common use among us developers because they save time and make you work faster. Here you have the shortcuts for Windows and Unix Systems (Linux and MacOS).

Using CTRL+z on Windows

Before pressing ctrl+z to exit Python just make sure your Python terminal is the selected windows, and then press ctrl, hold it down and press z and then Return. Python closes and you are back at your system prompt:

bash
>>> ^Z
 
C:\Users\you>

Using CTRL+d on MacOS and Linux

On Unix systems such as MacOS or Linux, ctrl+z will not exit Python on the terminal.

The keyboard shortcut for exiting Python on these systems would be ctrl + d and on these systems you don't need to confirm with return because their terminal will receive and execute this command immediately.

We will do as we did with the Windows shortcut. Press and hold ctrl and then press d to exit Python from the terminal making sure the terminal is the selected window.

You can read more related topics at 4Geeks like for example how to update python in terminal. Hope you enjoy the reading and keep on the Geek side!

Knowing how to enter and exit the Python interpreter is part of getting comfortable in the terminal. To keep building those skills step by step, a software development bootcamp combines Python projects with guidance from mentors.

Frequently asked questions

How do I stop a running Python script in the terminal?

Press Ctrl + C. It raises a KeyboardInterrupt exception that stops the program in Windows, macOS and Linux terminals, unless the code catches that exception. This is different from Ctrl + D or Ctrl + Z plus Enter, which only close the interactive interpreter when you are at an empty >>> prompt rather than interrupting code that is still running.

What is the difference between exit(), quit() and sys.exit()?

exit() and quit() are helpers added for the interactive interpreter and are not meant for programs. Inside scripts, use sys.exit() after import sys. All three raise SystemExit, but sys.exit() is always available and accepts an exit status, for example sys.exit(0) for success or sys.exit(1) to signal an error to the shell.

Why does Ctrl+Z show 'suspended' instead of closing Python on Mac or Linux?

On macOS and Linux, Ctrl + Z tells the shell to suspend the process, not to end it, so Python stays in the background as a paused job. Type fg to bring it back and then exit with exit() or Ctrl + D, or end it from the shell with kill %1, where the number matches the job shown in brackets.

How do I get out of help() or the ... prompt in Python?

Inside the help> utility, type quit or press Enter on an empty line to return to the >>> prompt; if a help page is open in a pager, press q first. The ... prompt means Python is waiting for the rest of a block: press Enter on an empty line to finish it, or Ctrl + C to cancel it.