Ctrl-C for quitting Python in Powershell now not working
Solution 1
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
Solution 2
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:
- 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.
- 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.
- Press
win key + r
to open the run application program. - Type
osk
and press ok - 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.
Solution 3
You can type
CTRL + Z,
then hit ENTER to exit python from powershell.
oblong
Updated on July 09, 2022Comments
-
oblong almost 2 years
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.
-
oblong over 7 yearsThanks 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 about 7 yearsThank you Dave. I'll look into this.
-
decoder over 3 yearsIn my case, I don’t have a break key in my laptop’s keyboard. So I use, Ctrl +Fn + B
-
KansaiRobot about 2 yearswhat is right ctrl?
-
KansaiRobot about 2 yearsCtrl+ScrLK in the virtual keyboard worked!