25

Python fails to quit when using Ctrl-C in Powershell/Command Prompt, and instead gives out a "KeyboardInterrupt" string.

Recently I've reinstalled Windows 10. Before the reinstall Ctrl-C quit python (3.5/2.7) fine, with no output.

Does anyone know why this has started happening? Whether it's just a simple setting?

The only difference I can think of is I'm now on python 3.6. Ctrl-D works in Bash on Ubuntu on Windows, and Ctrl-C works fine in an activated anaconda python2 environment for quitting python.

1
  • I also faced this kind of problem with a running wsl image export. I eventually managed to kill the process by simply closing the Powershell window. While it may not solve everybody's "Ctrl-C in Powershell" problem, it might help some of the readers. Dec 1, 2020 at 9:57

8 Answers 8

34

I had this issue with Windows 10 Pro Build 18363 and Python 3.8.1. I was running some python scripts and was unable to stop some with CTRL + C, but CTRL + BREAK worked every time. The Windows Docs had this to say:

The CTRL+C and CTRL+BREAK key combinations receive special handling by console processes. By default, when a console window has the keyboard focus, CTRL+C or CTRL+BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input...

CTRL+BREAK is always treated as a signal, but an application can change the default CTRL+C behavior in two ways that prevent the handler functions from being called:

  1. The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT input mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.
  2. When SetConsoleCtrlHandler is called with NULL and TRUE values for its parameters, the calling process ignores CTRL+C signals. Normal CTRL+C processing is restored by calling SetConsoleCtrlHandler with NULL and FALSE values. This attribute of ignoring or not ignoring CTRL+C signals is inherited by child processes, but it can be enabled or disabled by any process without affecting existing processes.

Thus, CTRL + C seems to be a SIGINT and its actions can be modified by the program you are running. It seems that Python on Windows has been coded in such a way that CTRL + C is being processed as keyboard input rather than the SIGINT we are expecting. Fortunately for me I have the CTRL + BREAK keys on my keyboard and this works every time.

For those of you who dont have BREAK on your keyboard, you can use the Windows On Screen virtual Keyboard.

  1. Press win key + r to open the run application program.
  2. Type oskand press ok
  3. On the virtual keyboard, press ctrl + ScrLk and this should kill the program.

This stack thread has some other methods you can try if ctrl + ScrLk doesnt work on the virtual keyboard.

2
  • 3
    In my case, I don’t have a break key in my laptop’s keyboard. So I use, Ctrl +Fn + B
    – decoder
    Aug 27, 2020 at 0:37
  • Ctrl+ScrLK in the virtual keyboard worked! Apr 17, 2022 at 14:08
14

You can type

CTRL + Z, 

then hit ENTER to exit python from powershell.

Powershell Screenshot

4

This is a bug that recently appeared in Windows 10 Insider build 15002.

A work around is to change the Mapped Keys from Ctrl C to something like Ctrl K

If you are not familar how to do this, You can look up or at stty -a

You can run this command on each bash session that will map your Terminate to Ctrl + K

stty intr \^k

As a TEMP solution you could include this in your Bashrc so it is executed on each new session

This bug has been reported already on Github #1569

1
  • Thank you Dave. I'll look into this.
    – oblong
    Mar 12, 2017 at 11:29
3

In my case, I found out that right ctrl + c does the trick in anaconda3 powershell - so no remapping necessary - I'm on Windows 10.

2
  • what is right ctrl? Apr 17, 2022 at 14:04
  • This works. Right ctrl is the right control key on your keyboard. There is one on the left too.
    – Hrvoje T
    Sep 1, 2022 at 20:55
2

That worked like magic

If you have a laptop with Fn key, tap:

Ctrl + Fn + S

Source: https://www.dell.com/community/Laptops-General-Read-Only/break-key-alternative/td-p/3826467

1

When you press Control C, a KeyboardInterrupt exception is thrown. If it isn't stopping the code, the best thing to do is to add a try statement into your code which catches KeyboardInterrupt

try:
    ....

except KeyboardInterrupt:
    exit()

The code is just off the top of my head so sorry if something is wrong.

EDIT: Ctrl Break or on some keyboards Ctrl Pause instantly stops python code apparently.

2
  • Thanks for the answer. But it isn't about code stopping. It's just about quitting python. When I used to press Ctrl-C, python was quit normally without any output. Why has this now changed to give just an output of KeyboardInterrupt? I'm not brilliant at this stuff so apologies for anything stupid.
    – oblong
    Feb 4, 2017 at 22:21
  • ctrl break is a good suggestion.
    – Andrew S
    Nov 22, 2023 at 16:14
0

Hitting Esc button on the upper corner of the keyboard seems to work for me on Windows-7, inside Spyder with numpy running, for Python 3.+ It broke the infinite ...: on an erroneous syntax in the interactive script

0

When I used to press Ctrl-C, python was quit normally without any output.

I really don't remember when I can quit python with Ctrl + C. In my memory, Ctrl + C always give me KeyboardInterrupt.


I also counldn't find an answer explaining why Ctrl + C can't quit the python in shell. FYI, Ctrl + D and Ctrl + \ can quit python for different reasons. See @Gilles's answer here. https://superuser.com/questions/169051/whats-the-difference-between-c-and-d-for-unix-mac-os-x-terminal


But I think how to handle Ctrl + C is merely a decision of the program. Yes, I think it totally depends on how the program would like to handle it. See the example I write below.

test.c

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

static void ctrlC_Handler(int sig){
    if(sig == SIGINT){
        printf("(:KeyboardInterrupt:)\n");
        printf(">>>");
        signal(SIGINT, ctrlC_Handler);
    }
}

static void welcome(){
    printf("Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)\n");
    printf("[GCC 7.3.0] on linux2\n");
    printf("Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n");
    printf(">>>");
    fflush(stdout);
}

int main(){
    welcome();
    signal(SIGINT, ctrlC_Handler);

    for(;;){
        pause();
    }

    return 0;
}

compile it with gcc test.c and execute it ./a.out. Try pressing Ctrl + C, you can see at least it mimics the behaviour(output) of the python program when you input Ctrl + C.

So, in my opinion, it's nothing special, but a human/programmer/author's decision on how to handle it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.