Why doesn't this python keyboard interrupt work? (in pycharm)

Edwin Shepherd picture Edwin Shepherd · Sep 30, 2016 · Viewed 24.8k times · Source

My python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in pycharm. My code look like this:

try:
    while loop:
        print("busy")

except KeyboardInterrupt:
    exit()

EDIT: There seems to be some problems with my slimmed down code working and not producing the same error. The full code can be viewed here. I have also re-slimed down the code (The code above) and it has produced the same error.

Answer

RawkFist picture RawkFist · Feb 25, 2020

I know this is an old question, but I ran into the same problem and think there's an easier solution:

In PyCharm go to "Run"/"Edit Configurations" and check "Emulate terminal in output console". PyCharm now accepts keyboard interrupts (make sure the console is focused).

Tested on: PyCharm 2019.1 (Community Edition)