Exiting Python Debugger ipdb

白昼怎懂夜的黑 提交于 2019-11-28 22:13:48

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/32055062/exiting-python-debugger-ipdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!