Is there any way to send a keyboard interrupt event in PyCharm IDE (3.1) while in the debugging mode?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I came through this message while searching Pycharm's bug tracking for this issue: https://youtrack.jetbrains.com/issue/PY-4840
If you are using version Pycharm 3, this might help, it worked for me.
One of the comments in the tracker: 'I have actually found out that Ctrl+C does eventually stop the running script, but you have to first write a letter into the console while it's running. So click into the console window, hit any key and then press Ctrl-C. In other words, it looks like a problem of GUI frame getting focus.'
As mentioned in this comment - Why doesn't this python keyboard interrupt work? (in pycharm):
Tested with PyCharm 2018.3 (Community Edition):
Also this will break tqdm library:
PyCharm Stop button now sends
SIGINT
both in debug mode and run mode. IfSIGINT
does not terminate the program, the Stop button changes its signal toSIGKILL
. It also changes its icon to a skull shape:This is a bug in PyCharm. See: http://youtrack.jetbrains.com/issue/PY-4840
Keyboard interrupt is a SIGINT. On unix systems you can either go to the command line and do:
or in python:
Unfortunately, there is no simple way to do this. You will need to use
psutil
and thesignal
module. For this to work you need to installpsutil
and the best way to do that is throughpip
:So, lets say we have here, exhibit A:
And you're running this in PyCharm. Make sure that the interpreter you're using has
psutils
installed. You can check:Make sure you've set your interpreter correctly:
If you haven't installed
psutil
, you can always do so though the Install button.Okay then, so now that we have everything set up, lets debug the program:
Now all we have to do is get the process ID, and we can get that at the very start of the program:
So, lets fire up our console, and send a signal:
And if that worked properly, you should see the while loop ending:
You can further streamline the process by adding a function to send an interrupt in the starting script for your console:
Once you're done with all of that, all you need to do is call
interrupt(<pid here>)
to call a keyboard interrupt on your process.I hope that answers your question.