How To Stop Python In Terminal

>>> exitworking with Python in the terminal, you may sometimes find yourself in a situation where you need to stop
the execution of a Python script or command. This may be because the script is taking too long, it’s caught in an
infinite loop, or you simply need to exit the Python interpreter. In this article, we’ll discuss how to
gracefully stop Python in the terminal.

Stopping a Python Script

To stop a currently running Python script in the terminal, press Ctrl+C. This sends an interrupt
signal to the script, causing it to stop its execution. If the script is not responding to the interrupt signal,
press Ctrl+C again, and it should eventually stop.

Exiting the Python Interpreter

If you are using the Python interpreter interactively (by typing python or python3 in
the terminal), you can exit the interpreter by typing exit() or quit() and pressing
Enter. Alternatively, you can press Ctrl+D (or Ctrl+Z on Windows) to
send an end-of-file (EOF) signal, which also exits the interpreter.


Stopping a Python Script

Let’s say you have the following Python script named

while True:
print(“This is an infinite loop!”)

To run this script, you would type the following in the terminal:


After running the script, you’ll see the message “This is an infinite loop!” printed repeatedly. To stop the
script, press Ctrl+C.

Exiting the Python Interpreter

If you are in the Python interpreter, you can exit it using one of the following methods:

  • Type exit() or quit() and press Enter
  • Press Ctrl+D (or Ctrl+Z on Windows)

For example, if you’re in the Python interpreter, you might see something like this:

>>> print(“Hello, World!”)
Hello, World!
>>> exit()

After typing exit() and pressing Enter, you will be returned to the terminal


Now you know how to stop a running Python script in the terminal and exit the Python interpreter. Remember to use
Ctrl+C to stop a script and exit(), quit(), or Ctrl+D (or
Ctrl+Z on Windows) to exit the interpreter. Happy coding!