Python - A keyboard command to stop infinite loop?

169,841

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.

Share:
169,841
PhillipKregg
Author by

PhillipKregg

I enjoy doing fun things with JavaScript, Ruby, Rails, and C# with ASP.NET MVC. I've also got a good bit of experience in UI/UX design and graphic design - if there were enough hours in the day I'd like to dive into some Django too. ... And every now and then I'm known to partake of some funky bass playing.

Updated on July 09, 2022

Comments

  • PhillipKregg
    PhillipKregg almost 2 years

    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.

  • PhillipKregg
    PhillipKregg over 12 years
    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.
  • mpen
    mpen over 12 years
    @Phillip: Don't know about Aptana, but most IDE's have a "Stop" or "Stop and Restart" button for console apps.
  • PhillipKregg
    PhillipKregg over 12 years
    Ok, thanks for the help - I was just hoping the keyboard shortcut would work within Aptana.
  • john ktejik
    john ktejik almost 8 years
    what if you executed the python script from rc.local? :(
  • J.M. Janzen
    J.M. Janzen over 7 years
    I've got nothing but an anecdotal "hunch" but I feel runaway printing responds more snappily to <C-c> when the -u command line flag (for unbuffering stdout/err) is set. Eg. python -u my-loop-script.py.
  • Kyle Delaney
    Kyle Delaney over 7 years
    Does Ctrl-C throw an exception that would mean it wouldn't work if it's in a try block?
  • ccpizza
    ccpizza over 6 years
    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.