use*_*728 7 debugging ipython python-2.7 pdb jupyter-notebook
我正在尝试在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:
Run Code Online (Sandbox Code Playgroud)
正如你所看到的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(只有我测试过的)可以正常工作。
我在 Jupyter Notebook 中调试时遇到了同样的问题。然而,对我有用的是当我在函数内调用 set_trace() 时。这里解释了为什么(点击),虽然我不太明白为什么其他人没有遇到这个问题。无论如何,如果您需要一个实用的解决方案来解决您的问题并且想要调试自己编写的函数,请尝试以下操作:
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'
Run Code Online (Sandbox Code Playgroud)
现在我可以使用“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)
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,直到有人可以完全解决问题。
| 归档时间: |
|
| 查看次数: |
487 次 |
| 最近记录: |