How to exit Python script in Command Prompt?

Question:

On previous computers, when I would try to exit a Python script on the Windows command prompt, all you need to do is press ctrl+c.

But when I do that on my computer it tells me "KeyboardInterrupt":

C:WindowsSystem32
>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> [I press ctrl+c]
KeyboardInterrupt
>>>

So how do I fix this so I can exit the Python script?

Thanks.

Edit:
ctrl+z works, but I need to enter it as code. Was hoping for a quick and easy way to just exit the script, but oh well.

Asked By: NBC

||

Answers:

It indeed depends on the OS, and probably on the version of Python you are using.

As you mentioned, ctrl+C does not work on your Windows 10 with Python 3.6, but it does work on my Windows 10 with Python 3.4. Therefore, you really need to try and see what works for you.

Try the following commands, and keep the one that works:

  • ctrl+C
  • ctrl+D
  • ctrl+Z then Return

In addition, the following should work with any terminal:

  • exit() then Return
  • quit() then Return

Trivia: if you type quit and hit Return, the console tells you, at least for Python 3.4:

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

Answered By: Right leg

You could simply type “quit()” and its done!
CTRL + C will interrupt a running script; but you only want to quit the interpreter. So quit() function will work for you.

Answered By: Ali Tohidi

ctrl+Z works still

..AppDataLocalProgramsPythonPython37>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
>>>
>>> (1+4)*2
10
>>> ^Z

For an embedded python (e.g. python-3.6.8-embed-win32) quit() command does not work

Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] on win32
>>> quit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'quit' is not defined

The only way that works is: CTRL + Z then Return.

Answered By: and1er

for windows 10 python 3.9 try to type exit()

Answered By: souad988

Use exit() or quit() if you’re using Windows(I found crtl combinations absolutely didnt work and ctrl+c gave me keyboard interrupt error)

Answered By: Sreelakshmi G

my case(Python 3.9.6 on win10):

1 running script from the console

server:
enter image description here
client:
enter image description here

2 stop script in Task Manager, select py.exe(not python.exe) and click the "end task" button.
enter image description here
enter image description here

Answered By: terwxqian
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.