21

Possible Duplicate:
Why can't I handle a KeyboardInterrupt in python?

I was playing around with some Python code and created an infinite loop:

y = 0
x = -4

itersLeft = x
while(itersLeft<0):
    y = y + x
    itersLeft = itersLeft - 1
    print "y = ",y, "itersLeft = ", itersLeft

print y

Is there a keyboard shortcut that would allow me to stop the looping - allowing me to fix the loop and then restart it?

I've tried Ctrl+C and didn't have any luck. If it helps I'm using a Windows 7 environment.

Thanks.

EDIT


I should have also mentioned that I'm using Aptana Studio 3 and attempted to run the Ctrl+C command within that. It doesn't work there - but trying it within the regular console works fine. I'm assuming it must be because of the Aptana environment.

plamut
  • 3,085
  • 10
  • 29
  • 40
PhillipKregg
  • 9,358
  • 8
  • 49
  • 63
  • That should do it. What did you use to run it? – jan zegan Dec 01 '11 at 00:48
  • press the red stop button on the eclipseIDE – Serdalis Dec 01 '11 at 01:02
  • 1
    Seeing that you are using Aptana Studio 3, is it at least 3.03? http://jira.appcelerator.org/browse/APSTUD-1469 What happened when you killed it with task manager? Have you tried ctrl+z? – Keller Scholl Dec 01 '11 at 00:55
  • 2
    Why the hell is this marked as a duplicate? That's just plain dumb, excluding cursing. The so-called "original" is about catching *KeyboardInterrupt* errors. – HarryCBurn Jun 22 '14 at 19:27

1 Answers1

33

Ctrl+C is what you need. If it didn't work, hit it harder. :-) Of course, you can also just close the shell window.

Edit: You didn't mention the circumstances. As a last resort, you could write a batch file that contains taskkill /im python.exe, and put it on your desktop, Start menu, etc. and run it when you need to kill a runaway script. Of course, it will kill all Python processes, so be careful.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
kindall
  • 178,883
  • 35
  • 278
  • 309
  • Thanks Kindall. I should also have mentioned that I'm using Aptana Studio 3 and attempted to use the Ctrl-C command there. It works fine in the regular terminal window, but not within Aptana. I actually had to restart Aptana in order to stop the loop. – PhillipKregg Dec 01 '11 at 00:53
  • @Phillip: Don't know about Aptana, but most IDE's have a "Stop" or "Stop and Restart" button for console apps. – mpen Dec 01 '11 at 01:00
  • Ok, thanks for the help - I was just hoping the keyboard shortcut would work within Aptana. – PhillipKregg Dec 01 '11 at 01:08
  • what if you executed the python script from rc.local? :( – john k Jun 18 '16 at 02:05
  • I've got nothing but an anecdotal "hunch" but I feel runaway printing responds more snappily to `` when the `-u` command line flag (for unbuffering stdout/err) is set. Eg. `python -u my-loop-script.py`. – J.M. Janzen Sep 13 '16 at 17:34
  • Does Ctrl-C throw an exception that would mean it wouldn't work if it's in a try block? – Kyle Delaney Dec 30 '16 at 02:11
  • On linux/osx you could use `pkill -f mynastyscript.py` — so you will only kill your script and not everything else. You don't have to include the extension if the name is unique enough since `-f` will match by any substring in the full command line. – ccpizza Nov 08 '17 at 20:46