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

Posted on

Question :

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

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:

    while loop:

except KeyboardInterrupt:

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 #1:

From your screen shot it appears that you are running this code in an IDE. The thing about IDEs is that they are not quite the same as running normally, especially when it comes to handling of keyboard characters. The way you press ctrl-c, your IDE thinks you want to copy text. The python program never sees the character. Pehaps it brings up a separate window when running? Then you would select that window before ctrl-c.

Answered By: Edwin Shepherd

Answer #2:

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)

Answered By: tdelaney

Answer #3:

PyCharm’s Python Console raises the exception console_thrift.KeyboardInterruptException on Ctrl-C instead of KeyboardInterrupt. The exception console_thrift.KeyboardInterruptException is not a subclass of KeyboardInterrupt, therefore not caught by the line except KeyboardInterrupt.

Adding the following lines would make your script compatible with PyCharm.

    from console_thrift import KeyboardInterruptException as KeyboardInterrupt
except ImportError:

This would not break compatibility with running the script in a terminal, or other IDE, like IDLE or Spyder, since the module console_thrift is found only within PyCharm.

Answered By: RawkFist

Answer #4:

If that comment doesn’t solve your problem, (from @tdelaney) you need to have your shell window focused (meaning you’ve clicked on it when the program is running.) and then you can use Control+C

Answered By: Friedrich S

Answer #5:

You can also use PyCharm’s Python console and use Ctrl + C, if you catch the exception that PyCharm raises when Ctrl + C is pressed. I wrote a short function below called is_keyboard_interrupt that tells you whether the exception is KeyboardInterrupt, including PyCharm’s. If it is not, simply re-raise it. I paste a simplified version of the code below.

When it is run:

  • type ‘help’ and press Enter to repeat the loop.
  • type anything else and press Enter to check that ValueError is handled properly.
  • Press Ctrl + C to check that KeyboardInterrupt is caught, including in PyCharm’s python console.

Note: This doesn’t work with PyCharm’s debugger console (the one invoked by “Debug” rather than “Run”), but there the need for Ctrl + C is less because you can simply press the pause button.

I also put this on my Gist where I may make updates: https://gist.github.com/yulkang/14da861b271576a9eb1fa0f905351b97

def is_keyboard_interrupt(exception):
# The second condition is necessary for it to work with the stop button
# in PyCharm Python console.
return (type(exception) is KeyboardInterrupt
or type(exception).__name__ == 'KeyboardInterruptException')

def print_help():
print("To exit type exit or Ctrl + c can be used at any time")

while True:
task = input("What do you want to do? Type "help"" for help:- "")

Leave a Reply

Your email address will not be published.