39

I use ipdb fairly often in a way to just jump to a piece of code that is isolated i.e. it is hard to write a real script that uses it. Instead I write a minimal test case with mocking and jump into it.

Exemplary for the workflow:

def func():
   ...
   import ipdb
   ipdb.set_trace()
   ...

def test_case():
    ...
    func()
    ...

Then, invoke

py.test test_file.py -s -k test_case

Now, usually I just check one variable or two, and then want to quit. Change the code and do it over again.

How do I quit? The manual says q quits the debugger. It doesn't (really). You have to quit a few times before the debugger actually terminates. The same behavior for Ctrl-C and Ctrl-D (with the additional frustration that hitting Ctrl-D several times eventually quits the terminal, too).

Is there a smart way to force quit? Is this workflow even sensible? What is the standard way to do it?

Joachim
  • 3,210
  • 4
  • 28
  • 43
  • 3
    I'm getting this now too. This is recent behavior. It used to ctrl-c just fine but now it just won't quit. even throwing an exception won't stop it. – Chris Sattinger Feb 12 '16 at 13:14
  • 2
    Link to [GitHub issue](https://github.com/gotcha/ipdb/issues/111) about this problem. – Christian Long Aug 31 '16 at 14:34
  • the original problem has been solved in ipython 5.2, see @cheflo's answer. Can you accept it as the answer not to confuse other people with the suggested workarounds? – Oleg Kuralenko Feb 26 '17 at 12:20

6 Answers6

54

The following worked for me:

import sys
sys.exit()

On newer versions of ipython, as mentioned above and below, this doesn't work. In that case,

import os
os._exit(0)

should still do the trick.

KDN
  • 1,349
  • 2
  • 14
  • 17
44

I put the following in my .pdbrc

import os

alias kk os.system('kill -9 %d' % os.getpid())

kk kills the debugger and (the process that trigger the debugger).

Joachim
  • 3,210
  • 4
  • 28
  • 43
  • 1
    It would work but looks like a workaround rather than a solution of the problem. Did you try to ask ipdb developers of it? BTW looks like a similar problem http://stackoverflow.com/questions/39090752/how-to-quit-ipdb-while-in-post-mortem-debugging – Oleg Kuralenko Aug 25 '16 at 06:54
  • Yes, I agree it is a work-around. One of the downsides of this work-around is that if you invoke the debugger from an ipython console with `%debug` and use my `kk` macro it will not just quit the debugger but also kill the ipython console. It should be raised with the developers. – Joachim Aug 31 '16 at 21:38
  • Where is the .pdbrc file? Do you have to create it if it doesn't exist already? – RubberDuckRabbit Mar 11 '17 at 00:09
  • Shouldn't be `alias kk import os; os.system('kill -9 %d' % os.getpid())` in case `os` is not imported? – alper Oct 25 '21 at 11:29
  • 1
    For Linux/Mac plus Windows let it be: `import os; import platform; terminate_cmd = 'kill -9 %d' % os.getpid() if platform.system() != 'Windows' else 'taskkill /F /PID %d' % os.getpid(); alias kk os.system(terminate_cmd)` on different lines – Jo Ja Feb 14 '23 at 14:12
7

It's the problem with the recent version of IPython 5.1.0. You can check with your environment using the following code:

pip freeze | egrep -i '^i'

It will be resolved by downgraded to IPython==5.0.0.

pip install ipython==5.0.0

That works for me.

Scofield77
  • 917
  • 10
  • 16
7

As mentioned in another answer, this was a bug in IPython 5.1. It was fixed in this pull request and is no longer an issue from IPython 5.2 and onwards. You can now use q, quit(), or Ctrl+d to exit the debugger.

joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • Just tested it and it works fine for me. I think it should be the accepted answer as from ipython 5.2 onwards the suggested workarounds don't make sense – Oleg Kuralenko Feb 26 '17 at 12:17
  • @DarkStar1 It works for me in a loop, can you post a sample of what does not work? – joelostblom May 16 '17 at 20:00
  • I'm using ipython 6.1.0 and ipdb 0.10.3; if the trace is set inside the loop, q just advances to the next iteration of the loop and I still have to use os._exit(0) – thumbtackthief Feb 12 '18 at 17:33
  • @thumbtackthief I just tested this again and it is still working for me to exit the debugger while inside a loop. Python 3.6.4, IPython 6.2.1, ipdb 0.11, Linux. Can you post an example of what is not working for you? – joelostblom Apr 11 '18 at 15:02
  • There is also this related issue: https://github.com/gotcha/ipdb/issues/111 – Ciro Santilli OurBigBook.com Nov 16 '18 at 12:11
2

Sloppy but effective way is to set monkey patch ipdb.set_trace = lambda:0, then every subsequent time ipdb.set_trace is hit it will do nothing and return to the calling function. So you won't have to type q any more.

JnBrymn
  • 24,245
  • 28
  • 105
  • 147
  • Very useful when you don't want to actually "kill" the process that called ipdb. This will keep you in the ipdb scope, but will run the parent process to completion. – Victor Valente Jun 30 '22 at 19:39
0

I've found these solutions only succeed in breaking your kernel, and then you have to restart and load everything again.

The problem I was having was in a for loop q will just proceed to the next iteration instead of quitting out of the loop. Eventually I figured out it only happens if your for loop is in a try statement. Remove the try and you can quit out of the debugger again without it continuing the for loop.

John Sakon
  • 31
  • 3