如何在Python中使用空闲之外的东西以交互方式调试异常?
当我使用IDLE运行脚本时,如果脚本遇到异常并停止执行,那么我将得到一个交互式shell,我可以使用它来调查异常时的应用程序状态。这真的很好,但我发现作为一名编辑,我缺少空闲时间。有没有一种方法可以在不使用IDLE的情况下获得这种“在异常情况下拖放到交互式shell”行为?如何在Python中使用空闲之外的东西以交互方式调试异常?,python,ide,python-idle,Python,Ide,Python Idle,当我使用IDLE运行脚本时,如果脚本遇到异常并停止执行,那么我将得到一个交互式shell,我可以使用它来调查异常时的应用程序状态。这真的很好,但我发现作为一名编辑,我缺少空闲时间。有没有一种方法可以在不使用IDLE的情况下获得这种“在异常情况下拖放到交互式shell”行为?python-I当yourscript退出时,yourscript将拖放到交互式shell。此时,您可以运行: >>> import pdb >>> pdb.pm() …并获得一个交互式调
python-I当yourscript
退出时,yourscript
将拖放到交互式shell。此时,您可以运行:
>>> import pdb
>>> pdb.pm()
…并获得一个交互式调试shell
请参阅。您可以使用
我建议使用eclipse和。它有很多调试选项,我不认为使用shell进行调试有什么好处。我说,试试看,你可能会发现。IMHO,如果你使用python而不使用它,那你就是在浪费时间(从字面意义上讲)
在其中,只需键入“magic”命令即可打开或关闭pdb。新的qtconsole(我最喜欢的)和笔记本选项使这个杀手级环境变得更好。从python命令解释器内部运行脚本(
import
it),当出现异常时,执行import pdb;pdb.pm()
以在引发异常后获取调试器。按如下方式运行脚本:
python -m pdb myscript.py
控制台将向您显示:
> /home/user/dir/myscript.py(2)<module>()
从现在开始,您基本上处于一种状态,应用的标准命令数量惊人
键入where或w查看您在堆栈上的位置:
(Pdb) w
-> return df[df['type']=='dev'][['Dist','Count']].as_matrix()
/home/user/core/ops.py(603)wrapper()
-> res = na_op(values, other)
> /home/user/core/ops.py(567)na_op()
-> raise TypeError("invalid type comparison")
看到那个小箭头了吗?这就是我们在堆栈中的位置
使用list或l环顾四周:
(Pdb) list
564 try:
565 result = getattr(x, name)(y)
566 if result is NotImplemented:
567 >> raise TypeError("invalid type comparison")
568 except (AttributeError):
569 -> result = op(x, y)
570
571 return result
572
573 def wrapper(self, other):
574 if isinstance(other, pd.Series):
要在堆栈中移动,请继续涂泥并使用向上(u)或向下(d)
使用args(a)检查调用当前函数的参数:
(Pdb) args
dat = array([], shape=(0, 3), dtype=float64)
dev_classes = {81, 82, 21, 22, 23, 24, 31}
使用p打印变量的内容(或pp打印(或处理角色的基本需要)):
使用交互在堆栈中的当前点输入代码Ctrl+D将您带回PDB
走吧!需要许多勇敢而强大的冒险家才能把聚集在城市周围的地精大军赶回来。你会成为打败地精王的人吗?你会为种族开垦土地吗?就是这样。我把它用在更小的零件上,我认为可能会出问题。如果你不喜欢,就接受查尔斯·达夫的建议。IPython是IDE吗?还是只是为了互动?
(Pdb) list
564 try:
565 result = getattr(x, name)(y)
566 if result is NotImplemented:
567 >> raise TypeError("invalid type comparison")
568 except (AttributeError):
569 -> result = op(x, y)
570
571 return result
572
573 def wrapper(self, other):
574 if isinstance(other, pd.Series):
(Pdb) args
dat = array([], shape=(0, 3), dtype=float64)
dev_classes = {81, 82, 21, 22, 23, 24, 31}
(Pdb) p df
Empty DataFrame
Columns: [Dist, type, Count]
Index: []