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

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

问题描述

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

   -  - 返回 -  

> < ipython-input-22-04c6f5c205d1>(3)< module>()
1导入IPython.core.debugger为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#重置我们的崩溃处理程序
- > 2884 sys.excepthook = old_excepthook
2885除了SystemExit为e:
2886如果结果不是无:


$你可以看到b $ b

n 命令,这是我从 pdb 文档中所理解的应该执行下一行(我假设 ipdb 只是 pdb 适合在IPython上工作,特别是因为我可以找不到专门指向 ipdb 的命令文档,而不是 pdb



s 也有同样的问题。这实际上就是我想做的 - 进入情节调用(根据我的理解,这就是 s 我应该这样做,但我得到的与我从 n 得到的完全相同。我也尝试了 r ,我也遇到了同样的问题。



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



我也试过% debug MultipleInstanceError )和 %% debug (不显示代码正在执行的行 - 只是说什么行,使用 s 不会进入该函数,只需运行该行。)



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



on plain IPython 控制台,IPython的 set_trace (只有我测试过的)工作正常。

解决方案

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



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

 来自IPython.core.debugger import set_trace 

def thisfunction(x):
x + = 2
返回x

set_trace()#在调用函数之前启动调试。
#在ipdb控制台中调用's'进入thisfunction会产生错误
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:\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:

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天全站免登陆