gpt4 book ai didi

python - 使用 ipython 和 %pdb 在调试 session 中访问变量

转载 作者:太空狗 更新时间:2023-10-29 21:30:02 24 4
gpt4 key购买 nike

我是 ipython 的新手,我正在尝试使用 ipython 来调试我的代码。我做了:

[1]: %pdb
Automatic pdb calling has been turned ON

然后

In [2]: %run mycode.py

在代码中,我有 1/0,所以它会引发异常并自动进入调试 session 。

ZeroDivisionError: float division

ipdb> variable
array([ 0.00704313, -1.34700666, -2.81474391])

所以我可以访问变量。但是当我执行以下操作时:

ipdb> b = variable
*** The specified object '= variable' is not a function or was not found along sys.path.

但这行得通:

ipdb> b = self.X

最佳答案

b 用于设置断点。因此 b 之后的任何内容都应该是函数或行号。

如果您键入 ipdb> help,您将看到完整的命令列表(保留字)。

例如,您可以使用 xy 作为变量:

ipdb> y = variable

ipdb> exec 'b = variable'

关于python - 使用 ipython 和 %pdb 在调试 session 中访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4187330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com