python - IPython (Jupyter) 笔记本中的交互式调试

标签 python debugging ipython jupyter-notebook ipdb

为了调试我的 python 代码,我使用 ipdb 库,并使用 set_trace() 命令放置一个断点。代码到达那里后,我会得到一个带有 ipdb> 提示符的交互式 shell,我可以使用 tab 自动完成功能探索局部变量。

然而,在 IPython (Jupyter) notebook 中,ipdb.set_trace() 不起作用。正如这篇文章所建议的: using ipdb to debug python code in one cell (jupyter or Ipython)

我使用以下替代方法进行交互式调试:

from IPython.core.debugger import Tracer
Tracer()() #this one triggers the debugger

这给了我 ipdb> 提示,但选项卡自动完成不可用。无论如何,是否可以使用 ipython notebook 为交互式调试启用自动完成功能?这非常有用,特别是当您有很多长名称的变量时。

最佳答案

在 Python 3.7 中你可以使用 breakpoint()功能

This function drops you into the debugger at the call site. Specifically, it calls sys.breakpointhook(), passing args and kws straight through. By default, sys.breakpointhook() calls pdb.set_trace() expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly import pdb or type as much code to enter the debugger. However, sys.breakpointhook() can be set to some other function and breakpoint() will automatically call that, allowing you to drop into the debugger of choice.

关于python - IPython (Jupyter) 笔记本中的交互式调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490412/

相关文章:

python - 如何在 tensorflow 中对张量的列执行相似函数

debugging - MiniDumpWriteDump() 函数的参数 : Why do we need a handle and an ID?

debugging - 在Qt中设置断点后,gdb显示: "Error accessing memory address"

python - 如何在 Jupyter notebook (iPython) 中并排显示 2 张图像?

python - ipython 0.10.1 中的意外缩进

python - PyQt:把滚动条放在这

python - 如何读取文件夹中的所有文本文件内容并将这些内容作为行复制到一个 excel 文件中

python - os.error 有什么意义?

java - 无法通过 Eclipse 调试 Tomcat

python - 使用 Pandas 绘制时间戳无法按预期工作