IPython(Jupyter)noteb中的交互式调试

2024-04-27 09:20:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

然而,在IPython(Jupyter)笔记本中,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笔记本的交互式调试是否有自动完成功能?这是非常有用的,特别是当有很多变量具有长名称时。在


Tags: 代码命令功能ipythontrace笔记本shelldebugger
1条回答
网友
1楼 · 发布于 2024-04-27 09:20:02

在Python3.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.

相关问题 更多 >