JupyterLab 中的 pdb 未进入交互模式
pdb in JupyterLab not entering interactive mode
我的目标是在 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 notebook 也是如此。
我尝试过的事情:
- 重新启动 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)
:
希望这个h(elps)!
编辑:
OP 提供了以下说明:
What I am actually looking for is a proactive way to insert break
points, i.e., how to insert breakpoint even if there isn't any error.
在这种情况下,您可以将 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
,我希望你会得到你正在寻找的东西。无论如何,我希望这能回答 'breakpoint without an error' 部分。
我不打算包括 运行ning bt
的全部输出,因为它很少。但这里是 运行ning ?bt
的输出,以获取有关该特定命令的更多帮助:
你可以用 %运行 魔术命令来完成。
%run ./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
我的目标是在 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 notebook 也是如此。
我尝试过的事情:
- 重新启动 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)
:
希望这个h(elps)!
编辑:
OP 提供了以下说明:
What I am actually looking for is a proactive way to insert break points, i.e., how to insert breakpoint even if there isn't any error.
在这种情况下,您可以将 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
,我希望你会得到你正在寻找的东西。无论如何,我希望这能回答 'breakpoint without an error' 部分。
我不打算包括 运行ning bt
的全部输出,因为它很少。但这里是 运行ning ?bt
的输出,以获取有关该特定命令的更多帮助:
你可以用 %运行 魔术命令来完成。
%run ./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