如何在Jupyter notebook中debug?

2019-08-03  本文已影响0人  琼脂糖

notebook 中内建的pdb

在需要breakpoint的地方插入import pdb; pdb.set_trace(),运行后会进入debugger,有一个交互界面。

def test_breakpoint_with_ipdb():
    a = 1
    import pdb; pdb.set_trace()
    b = 2
    c = 3
    final = a + b + c
    return final

test_breakpoint_with_ipdb()
image.png
debugger会在断点前停下, n执行下一行,c执行下面所有代码。h可以查看所有命令。
image.png

ipdb

from IPython.core.debugger import set_trace
def test_breakpoint_with_ipdb():
    a = 1
    set_trace()
    b = 2
    c = 3
    final = a + b + c
    return final

test_breakpoint_with_ipdb()
image.png

如果遇到报错更新一下ipython:
conda update ipython
conda update ipykernel

参考

1.Debugging Jupyter notebooks

上一篇 下一篇

猜你喜欢

热点阅读