43

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:\Windows\System32
>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.

7
  • 3
    ctrl+c stops any process currently running. Use quit() to exit the console. Commented Jan 7, 2017 at 18:07
  • press <CTR>+d to exit Commented Jan 7, 2017 at 18:07
  • 2
    Try typing in 'exit()' instead. Commented Jan 7, 2017 at 18:07
  • 1
    @elyashiv that doesn't work on Windows (at least not on my machine - Windows 10 with Python 3.6) Commented Jan 7, 2017 at 18:08
  • 2
    @3novak, Python has a Ctrl+C handler that defaults to raising a KeyboardInterrupt exception. That it's not working in Windows 8+ prior to 3.6 is due to a bug in Windows, as I've explained below. The control event that we never handle automatically is Ctrl+Break. That will immediately and ungraciously terminate the process. It's a crude way to exit a process in general, and should only be used as a last resort. Commented Jan 8, 2017 at 0:47

8 Answers 8

66

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

Sign up to request clarification or add additional context in comments.

4 Comments

Ctrl+C only "works" (it's a bug) in Windows 8+ prior to Python 3.6 because the Windows ReadFile implementation broke its API contract to set the last error to ERROR_OPERATION_ABORTED when reading from the console is interrupted by Ctrl+C. Without that, Python's REPL thinks the read successfully returned 0 bytes, i.e. end of file, and exits. 3.6 switched to calling ReadConsoleW to better support Unicode, and this function sets the proper error for a read interrupted by Ctrl+C.
ReadFile returns EOF if the buffer read from the console starts with Ctrl+Z, but it's a cooked read so you have to press enter to complete the read. ReadConsole doesn't implement this hack, so Python's REPL in 3.6 implements this on its own. But since it's using ReadConsole, I think it should also use the pInputControl parameter to support an immediate Ctrl+D, similar to a Unix terminal.
exit and quit are provided by the site module. They're not available if you run Python with the -S option that prevents importing site. They're a convenience for the REPL. A script should instead rely on sys.exit([exit_code]) or raise SystemExit([exit_code]).
On Python-3.6.1 for Windows 7, exit() and quit() do not work and produce the error NameError: name 'exit' is not defined. Ctrl+Z and Enter still do work.
10

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.

Comments

4

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.

Comments

4

To his problem: How to close a python server(.py) or client(.py) in the cmd window.

The answer is: cntrl + Pause or Break (Usually somewere above the left/up/down/right keys)

If that is not on your keyboard, close the cmd window and restart the server.

Eryk Sun already mentioned it, but for some reason his post isnt directly visible.

1 Comment

This was the one I was looking for. In case you're unable to invoke Ctrl+C for some reason, using Ctrl+Pause/Break will effectively do the same: It throws in a ^C character in the terminal.
2

ctrl+Z works still

..\AppData\Local\Programs\Python\Python37>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

1 Comment

Please add some context to the code snippet for the benefit of the future readers.
1

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

1 Comment

This is already mentioned in the accepted answer
1

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

Comments

-2

for windows 10 python 3.9 try to type exit()

1 Comment

exit() was already mentioned in the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.