Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctrl-C for quitting Python in Powershell now not working

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.

like image 533
oblong Avatar asked Feb 04 '17 10:02

oblong


3 Answers

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.

like image 109
Rex Linder Avatar answered Oct 26 '22 16:10

Rex Linder


You can type

CTRL + Z, 

then hit ENTER to exit python from powershell.

Powershell Screenshot

like image 12
harshaj Avatar answered Oct 26 '22 15:10

harshaj


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

like image 4
Dave Hamilton Avatar answered Oct 26 '22 16:10

Dave Hamilton