使用 ipdb 调试一个单元格中的 python 代码(jupyter 或 Ipython)

using ipdb to debug python code in one cell (jupyter or Ipython)

我正在使用带有 firefox 的 jupyter(或 Ipython)笔记本,并且想在单元格中调试一些 python 代码。我使用 'import ipdb; ipdb.set_trace()' 作为断点,例如我的单元格具有以下代码:

a=4
import ipdb; ipdb.set_trace()
b=5
print a
print b

使用 Shift+Enter 执行后出现此错误:

--------------------------------------------------------------------------
MultipleInstanceError                     Traceback (most recent call last)
<ipython-input-1-f2b356251c56> in <module>()
      1 a=4
----> 2 import ipdb; ipdb.set_trace()
      3 b=5
      4 print a
      5 print b

/home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__init__.py in <module>()
     14 # You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
     15 
---> 16 from ipdb.__main__ import set_trace, post_mortem, pm, run, runcall, runeval, launch_ipdb_on_exception
     17 
     18 pm                       # please pyflakes

/home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__main__.py in <module>()
     71         # the instance method will create a new one without loading the config.
     72         # i.e: if we are in an embed instance we do not want to load the config.
---> 73         ipapp = TerminalIPythonApp.instance()
     74         shell = get_ipython()
     75         def_colors = shell.colors

/home/nnn/anaconda/lib/python2.7/site-packages/traitlets/config/configurable.pyc in instance(cls, *args, **kwargs)
    413             raise MultipleInstanceError(
    414                 'Multiple incompatible subclass instances of '
--> 415                 '%s are being created.' % cls.__name__
    416             )
    417 

MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.

如果我不在浏览器的jupyter notebook中使用这段代码,而是在jupyter qtconsole中使用,也会出现同样的错误。 这个错误是什么意思以及如何避免它? 是否可以使用 pdb 调试器的 next、continue 等命令逐步调试单元格中的代码?

也遇到过这个问题,好像和jupyter和ipdb的版本有关

解决方案是使用这个代替 ipdb 库 set_trace 调用:

from IPython.core.debugger import Tracer
Tracer()() #this one triggers the debugger

来源:http://devmartin.com/blog/2014/10/trigger-ipdb-within-ipython-notebook/

带注释的截图:

如果使用 Jupyter Notebook 使用魔术命令“%%debug”开始您的单元格。 然后 ipdb 行将显示在单元格的底部,这将帮助您浏览调试会话。以下命令应该可以帮助您入门:

n- 执行当前行并转到下一行。

c- 继续执行直到下一个断点。

确保每次决定调试时重新启动内核,这样所有变量都是新鲜的 assigned.You 可以通过 ipdb 行检查每个变量的值,你会看到变量未定义直到您执行为该变量赋值的行。

%%debug
import pdb
from pdb import set_trace as bp
def function_xyz():
    print('before breakpoint')
    bp() # This is a breakpoint.
    print('after breakpoint')

我的Jupyter版本是5.0.0,对应的ipython版本是6.1.0。我正在使用

import IPython.core.debugger
dbg = IPython.core.debugger.Pdb()
dbg.set_trace()

Tracer 已弃用。

更新:

我尝试使用下面另一个答案 中的方法,但出现错误:

MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.

比起 %%debug 魔术,我更喜欢我的方法,因为我可以在其他单元格中定义的函数中设置断点,并 运行 另一个单元格中的函数。 Jupyter/IPython 在设置断点的函数中放入调试器,我可以使用通常的 pdb 命令。每个人都有自己的...

@lugger1,已接受的答案已弃用。

Tracer() 已弃用。

使用:

from IPython.core.debugger import set_trace

然后在需要断点的地方放置set_trace()

from IPython.core.debugger import set_trace

def add_to_life_universe_everything(x):
    answer = 42
    set_trace()
    answer += x

    return answer

add_to_life_universe_everything(12)

与仅使用内置 pdb 相比,它工作得很好并且给我们带来了更多的舒适感(例如语法高亮显示)。

source