34

Is there any way to send a keyboard interrupt event in PyCharm IDE (3.1) while in the debugging mode?

arbulgazar
  • 1,931
  • 1
  • 19
  • 24
  • I need the same thing. I need to be able to gracefully stop a script with handling code using CTRL-C or the like, not just use the Stop button. – fantabolous Apr 28 '14 at 06:10
  • Apparently not: see and go vote for [this](https://youtrack.jetbrains.com/issue/CPP-3067?_ga=2.210591317.1550189038.1594127255-2002604257.1583301113) - it's for CLion, but apparently if changed there, it'll also change PyCharm. – drevicko Jul 08 '20 at 01:45
  • Note also that in recent versions, the big red square sends SIGINT, waits one second, then SIGTERM. If your cleanup is quick enough, that may work for you. – drevicko Jul 08 '20 at 01:46

6 Answers6

23

Unfortunately, there is no simple way to do this. You will need to use psutil and the signal module. For this to work you need to install psutil and the best way to do that is through pip:

pip install psutil

So, lets say we have here, exhibit A:

while True:
    try:
        time.sleep(3)
        print "Zzzz"
        time.sleep(3)
        print("gong!")
    except KeyboardInterrupt as e:
        print "Closed by an Interrupt"
        break

And you're running this in PyCharm. Make sure that the interpreter you're using has psutils installed. You can check:

enter image description here

Make sure you've set your interpreter correctly:

enter image description here

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:

enter image description here

Now all we have to do is get the process ID, and we can get that at the very start of the program:

enter image description here

So, lets fire up our console, and send a signal:

enter image description here

And if that worked properly, you should see the while loop ending:

enter image description here

You can further streamline the process by adding a function to send an interrupt in the starting script for your console:

enter image description here

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.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
13

PyCharm Stop button now sends SIGINT both in debug mode and run mode. If SIGINT does not terminate the program, the Stop button changes its signal to SIGKILL. It also changes its icon to a skull shape:

See how the stop button changed its icon

milaniez
  • 1,051
  • 1
  • 9
  • 21
  • Any thoughts on how to always send SIGKILL? The little skull appears every time I try to stop debugging, it's quite bothersome. – MikeyE Mar 09 '17 at 11:02
  • @MikeyE Sending SIGKILL by default is a bad idea. It might leave child processes running and avoid cleanups. Anyhow if you decided that you need it anyhow, you can try closing the program (cross button, i.e, Ctrl+F4). – milaniez Mar 10 '17 at 02:13
  • It might be a bad idea, but that's what PyCharm does by default if the process can't be stopped. In my case, that's literally every time I press the stop button. Maybe sending a different signal would be better. But regardless, how do you do it in PyCharm? – MikeyE Mar 12 '17 at 03:26
  • 2
    @milaniez Do you know in which version is this feature introduced? It seems SIGINT is sent on my Mac, but with Windows PyCharm 2017.1.5 the program is terminated immediately in both debug and run mode. – wlnirvana Sep 21 '17 at 02:37
  • Note that there is a one second delay before SIGKILL is sent. If cleanup takes more than that, you're out of luck ): However sending SIGINT to the process (either with a terminal or with psutils as per GamesBraniac's answer) works also. – drevicko Jul 08 '20 at 01:41
12

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:

$ kill -INT <pid>

or in python:

import os, signal
os.kill(<pid>,signal.SIGINT)
dvj
  • 367
  • 6
  • 10
  • 1
    The bug for PyCharm was closed as fixed - attempting to get reopened. – Mark Aug 17 '15 at 02:19
  • Nice and simple solution here, this answer needs more upvotes. – David Parks May 09 '19 at 23:52
  • @Mark note (and go vote for!) https://youtrack.jetbrains.com/issue/CPP-3067?_ga=2.210591317.1550189038.1594127255-2002604257.1583301113 ... for CLion, but apparently if changed there, it'll also change PyCharm. – drevicko Jul 08 '20 at 01:43
9

As mentioned in this comment - Why doesn't this python keyboard interrupt work? (in pycharm):

In recent versions of PyCharm, you can enable Emulate terminal in output console in your Run Configuration - this allows Ctrl + C in the Run console to send a keyboard interrupt.

Tested with PyCharm 2018.3 (Community Edition):

enter image description here

Also this will break tqdm library:

enter image description here

Comrade Che
  • 619
  • 8
  • 25
5

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.'

damaZhang
  • 211
  • 4
  • 4
0

Sounds silly but this worked for me.

Exit PyCharm and delete .idea folder from finder and startup PyCharm again.

Maulzey
  • 4,100
  • 5
  • 22
  • 30