【问题标题】:How can I interactively debug exceptions in Python using something besides IDLE?如何使用 IDLE 以外的东西交互式调试 Python 中的异常?
【发布时间】:2012-03-20 16:10:23
【问题描述】:

当我使用 IDLE 运行脚本时,如果脚本遇到异常并停止执行,那么我会留下一个交互式 shell,我可以使用它来调查异常时的应用程序状态。这真的很好,但除此之外,我发现 IDLE 缺乏作为编辑器。有没有办法在不使用 IDLE 的情况下获得这种“在异常情况下进入交互式 shell”的行为?

【问题讨论】:

    标签: python ide python-idle


    【解决方案1】:

    我建议使用带有pydev 的eclipse。它有很多调试选项,我看不出使用 shell 进行调试有什么好处。试试吧,我说,你可以。

    【讨论】:

      【解决方案2】:

      按如下方式运行您的脚本:

      python -m pdb myscript.py
      

      控制台会显示:

      > /home/user/dir/myscript.py(2)<module>()
      

      -> first_line(of_my_script) (pdb)

      输入继续

      等待事情爆炸:

      TypeError: invalid type comparison
      Uncaught exception. Entering post mortem debugging
      Running 'cont' or 'step' will restart the program
      > /home/user/problemscript.py(567)na_op()
      -> raise TypeError("invalid type comparison")
      (Pdb)
      

      从现在开始,您基本上处于MUD 中,并且应用了数量惊人的标准命令。

      输入 wherew 以查看您在堆栈中的位置:

      (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")
      

      看到那个小&gt; 箭头了吗?这就是我们在堆栈中的位置。

      使用 listl 环顾四周:

      (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):
      

      要在堆栈中移动,请继续 MUDing 并使用 up (u) 或 down (d) .

      使用 args (a) 来检查调用当前函数的参数:

      (Pdb) args
      dat = array([], shape=(0, 3), dtype=float64)
      dev_classes = {81, 82, 21, 22, 23, 24, 31}
      

      使用 p 打印出变量的内容(或使用 pp 进行漂亮打印(或处理角色的基本需求)):

      (Pdb) p df
      Empty DataFrame
      Columns: [Dist, type, Count]
      Index: []
      

      使用 interact 在堆栈的当前点输入代码。 Ctrl+D 带您回到 PDB。

      前进!需要许多勇敢而强大的冒险家才能击退聚集在城市周围的地精部落。你会成为击败哥布林王,为种族夺回土地的人吗?

      【讨论】:

        【解决方案3】:

        python -i yourscript 将在yourscript 退出时进入交互式 shell。此时可以运行:

        >>> import pdb
        >>> pdb.pm()
        

        ...并获得一个交互式调试外壳。

        the PDB documentation

        【讨论】:

          【解决方案4】:

          您可以使用pdb module

          import pdb
          try:
              i = 0
              i = i + 'a string'
          except Exception, err:
              pdb.set_trace()
          

          【讨论】:

          • 就是这样。我将它用于较小的部分,我认为可能会出现问题。如果您不喜欢这样,请接受 Charles Duffs 的建议。
          【解决方案5】:

          从 python 命令解释器 (import it) 内部运行您的脚本,当出现异常时执行 import pdb; pdb.pm() 以在引发异常后的时间点获取调试器。

          【讨论】:

            【解决方案6】:

            恕我直言,如果你在 python 中工作而不使用IPython,那么你就是在浪费你的时间(从字面上看)。

            在其中,您只需键入“魔术”命令pdb 即可打开或关闭 pdb。新的 qtconsole(我最喜欢的)和笔记本选项使这个杀手级环境变得更好。

            【讨论】:

            • IPython 是 IDE 吗?还是仅用于互动内容?
            猜你喜欢
            • 1970-01-01
            • 2019-12-23
            • 1970-01-01
            • 2013-06-19
            • 2012-10-21
            • 2012-04-07
            • 1970-01-01
            • 1970-01-01
            • 2010-09-18
            相关资源
            最近更新 更多