我的目标是在JupyterLab中使用pdb
或ipdb
运行一些python脚本,以捕获我的调试历史记录。
我首先在我的python脚本中插入了set_trace()
:
import torch
from IPython.core.debugger import set_trace
def train_batch(model_instance, inputs_source, labels_source, inputs_target, optimizer, args):
inputs = torch.cat((inputs_source, inputs_target), dim=0)
total_loss, train_stats = model_instance.get_loss(inputs, labels_source)
total_loss[args.train_loss].backward()
set_trace() # insert breakpoint
optimizer.step()
return train_stats
然后,我在JupyterLab中运行以下脚本:
!python ./trainer/train.py \
--base_config ./config/sgd_vannila_0.001.yml \
--dataset Office-Home \
--class_num 50 \
--src_address ./data/office-home/Art.txt \
--tgt_address ./data/office-home/Clipart.txt \
--name transfer.debug.rand_noise \
--train_steps 10000 \
--seed 2 \
--filter_classes=0,50 \
--eval_interval 50
执行在断点处停止,但不提供提示任何
ipdb
指令的交互式框。 pdb
或jupyter笔记本也是如此。我尝试过的事情:
重新启动Chrome浏览器或笔记本电脑无济于事
在笔记本代码块中添加断点是可行的(请参见下面的屏幕截图),但是我希望能够调试在我的python模块文件中编写的代码
版本信息:
ipdb-0.12.2
Python 3.6.9
JupyterLab 0.35.5
最佳答案
我可能不在这里,但是我认为magic function %debug
是您要寻找的。尝试将下面的代码片段插入jupyterlab单元中并运行它:
def foo(a,b):
return(a+b)
c = foo(a=1, b=str(1))
这产生一个
TypeError
:现在,如果您在下面插入一个单元格,请输入
%debug
并运行它,您将得到以下信息:现在,您可以运行任何ipdb command,例如
h(elp)
:希望这可以帮助)!
编辑:
OP提供了以下澄清:
我实际上正在寻找的是一种主动插入中断的方法
点,即即使没有错误也如何插入断点。
在这种情况下,将
from IPython.core.debugger import set_trace
与ipdb命令bt
结合使用。这是一个例子:from IPython.core.debugger import set_trace
def foo(a,b):
return(a+b)
set_trace()
c = foo(a=1, b=1)
这将触发以下内容:
现在,运行命令
bt
,希望您得到的正是您想要的内容。无论如何,我希望这能回答“没有错误的断点”部分。我将不包括运行
bt
的全部输出,因为它很少。但是,这是运行?bt
以获得有关该特定命令的更多帮助的输出:关于python - JupyterLab中的pdb未进入交互模式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58031628/