【问题标题】:Debuggers not acting properly on Jupyter notebooks调试器无法在 Jupyter 笔记本上正常运行
【发布时间】:2018-03-11 17:44:40
【问题描述】:

我正在尝试在 Jupyter 笔记本中调试一些代码。我尝试了 3 4 种不同的方法,它们都遇到了同样的问题:

--Return--
None
> <ipython-input-22-04c6f5c205d1>(3)<module>()
      1 import IPython.core.debugger as dbg
      2 dber = dbg.Tracer()
----> 3 dber()
      4 tst = huh.plot(ret_params=True)
      5 type(tst)

ipdb> n
> y:\miniconda\lib\site-packages\ipython\core\interactiveshell.py(2884)run_code()
   2882             finally:
   2883                 # Reset our crash handler in place
-> 2884                 sys.excepthook = old_excepthook
   2885         except SystemExit as e:
   2886             if result is not None:

如您所见,n 命令,根据我从pdb 文档中的理解,应该执行下一行(我假设ipdb 只是pdb 适合在IPython 上工作,尤其是因为我找不到任何专门引用 ipdb 而不是 pdb 的命令文档)

s 也有同样的问题。这实际上是我想做的——进入plot 调用(据我了解,这是s 应该做的),但我得到的和我从n 得到的完全一样.我也刚试过r,我也遇到了同样的问题。

我看到的每个示例都只是使用Tracer()()IPython.core.debugger.PDB().set_trace() 在命令后面的行中设置断点,但两者都会导致相同的问题(而且,我认为实际上是完全相同的事情)。

我还尝试了%debug (MultipleInstanceError) 和 %%debug(不显示正在执行的行中的代码 - 只是说哪一行,使用 s 不会进入函数,只是运行行)。

编辑:事实证明,根据今年 4 月的一篇博客文章,普通的 pdb 也应该可以工作。它确实允许我以交互方式调试笔记本,但它只打印正在调试的当前行(可能不是错误),并且与 IPython 的set_trace()Tracer()() 存在相同的问题

在普通的IPython 控制台上,IPython 的set_trace(我只测试过一个)工作得很好。

【问题讨论】:

    标签: python-2.7 debugging ipython jupyter-notebook pdb


    【解决方案1】:

    我在 Jupyter Notebook 中调试时遇到了同样的问题。然而,对我有用的是当我在函数中调用 set_trace() 。为什么在这里解释(click),虽然我不太明白为什么其他人没有遇到这个问题。无论如何,如果你需要一个实用的解决方案来解决你的问题,并且你想调试一个自写的函数,试试这个:

    from IPython.core.debugger import set_trace
    
    def thisfunction(x):
        set_trace()        # start debugging when calling the function
        x += 2
        return x
    
    thisfunction(5)        # ipdb console opens and I can use 'n'
    

    现在我可以使用 'n' 并且调试过程运行下一行没有问题。但是,如果我使用以下代码,则会遇到上述问题。

    from IPython.core.debugger import set_trace
    
    def thisfunction(x):
        x += 2
        return x
    
    set_trace()            # start debugging before calling the function. 
                           # Calling 's' in the ipdb console to step inside "thisfunction" produces an error
    thisfunction(5)        
    

    希望这会有所帮助,直到有人可以完全解决问题。

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 2017-05-25
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多