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

前端 未结 7 1233
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 15:28

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:



        
相关标签:
7条回答
  • 2020-12-03 15:54

    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.

    try:
        from console_thrift import KeyboardInterruptException as KeyboardInterrupt
    except ImportError:
        pass
    

    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.

    0 讨论(0)
提交回复
热议问题