调试器在 Jupyter 笔记本上运行不正常 [英] Debuggers not acting properly on Jupyter notebooks

查看:21
本文介绍了调试器在 Jupyter 笔记本上运行不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

--返回--没有任何><ipython-input-22-04c6f5c205d1>(3)<module>()1 导入 IPython.core.debugger 作为 dbg2 dber = dbg.Tracer()---->3 dber()4 tst = huh.plot(ret_params=True)5型(tst)ipdb>n>y:minicondalibsite-packagesipythoncoreinteractiveshell.py(2884)run_code()2882​​终于:2883 # 重置我们的崩溃处理程序到位->2884 sys.excepthook = old_excepthook2885 除了 SystemExit 为 e:2886 如果结果不是 None:

如您所见,n 命令,根据我从 pdb 文档中的理解,应该执行下一行(我假设 ipdb 只是适用于 IPython 的 pdb,特别是因为我找不到任何专门引用 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(我只测试过一个)工作得很好.

解决方案

我在 Jupyter Notebook 中调试时遇到了同样的问题.然而,对我有用的是当我在函数内调用 set_trace() 时.为什么在这里解释

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

from IPython.core.debugger import set_trace定义这个函数(x):x += 2返回 xset_trace() # 在调用函数之前开始调试.# 在 ipdb 控制台中调用 's' 进入thisfunction"会产生错误这个函数(5)

希望这会有所帮助,直到有人完全解决问题为止.

I'm trying to debug some code in a Jupyter notebook. I've tried 3 4 different methods, and they all suffer from the same problem:

--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:minicondalibsite-packagesipythoncoreinteractiveshell.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:

as you can see, the n command, which from what I understood from the pdb documentation should execute the next line (I'm assuming ipdb is just pdb adapted to work on IPython, especially since I can't find any command documentation that refers specifically to ipdb and not pdb)

s also has the same problem. This is actually what I want to do - step into the plot call (from what I understand, this is what s is supposed to do), but what I get is exactly the same as what I get from n. I also just tried r and I get the same problem.

Every example I've seen just uses Tracer()() or IPython.core.debugger.PDB().set_trace() to set a breakpoint in the line that follows the command, but both cause the same problems (and, I assume, are actually the exact same thing).

I also tried %debug (MultipleInstanceError) and %%debug (Doesn't show the code in the line being executed - just says what line, using s doesn't step into the function, just runs the line).

Edit: turns out, according to a blog post from April of this year, plain pdb should also work. It does allow me to interactively debug the notebook, but it only prints the current line being debugged (probably not a bug), and it has the same problem as IPython's set_trace() and Tracer()()

on plain IPython console, IPython's set_trace (only one I've tested) works just fine.

解决方案

I encountered the same problem when debugging in Jupyter Notebook. What is working for me however, is when I call set_trace() inside a function. Why is explained here (click), though I don't really understand why others don't encounter this problem. Anyway, if you need a pragmatic solution for your problem and you want to debug a self-written function, try this:

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'

Now I can use 'n' and the debugging process runs the next line without problems. If I use the following code, however, I run into your above mentioned problem.

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)        

Hope this helps until somebody could solve the problem completely.

这篇关于调试器在 Jupyter 笔记本上运行不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆