Close Python In Terminal: Command & Shortcut

How do you quit out of Python when you’re in an interactive shell environment?

The easiest way to close the Python shell in Terminal is to issue the quit() command.

Here’s an example demonstrating how to exit from a terminal window using the a-Shell app on the iPhone:

Use the exit() command

As you can see from the example above I was able to access the Python interactive shell by entering python3 on the command line. This simple instruction invokes the Python REPL and enables the user to enter Python commands.

In this example, I’ve instructed Python to output the current working directory.

To exit from the interactive shell there are a variety of ways, and in this instance I exited by using the exit() command.

Here is another way of being able to exit the shell using a similar command called quit() :

Use the quit() command

Both commands perform the same operation by quitting the current interactive shell environment in a terminal window.

Use Keyboard Shortcut

There is another easier way of being able to exit the interactive shell within a terminal window and that is by using the Ctrl-D keyboard shortcut.

To use this shortcut simply hold down the Ctrl key on your keyboard and the tap the D keyboard key.

Summary

If you have opened the Python interactive shell from within a terminal window you can try one of 3 means of exiting out from the Python shell. On a new prompt row enter the command exit() or quit() , or use the keyboard shortcut Ctrl+D .

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.