Python调试工具的建议? [英] Suggestions for Python debugging tools?

查看:108
本文介绍了Python调试工具的建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我用Python进行了模拟。我有一些困难与变量和调试



是否有任何Python的软件,提供了一个体面的调试器?



相关问题:调试我的Python代码的最好方法是什么?

解决方案

不要忘了验证调试!抛出异常后,所有本地的堆栈框架都包含在 sys.last_traceback 中。您可以执行 pdb.pm()转到抛出异常的堆栈框架,然后p(retty)p(rint) localals )



这是一个使用此信息从堆栈中提取局部变量的函数。

  def findlocals(search,startframe = None,trace = False):

来自pprint import pprint
import inspect,pdb

startframe = startframe或sys.last_traceback
frames = inspect.getinnerframes(startframe)

frame = [tb,_,lineno,fname,_, _)在框架
如果搜索(lineno,fname)] [0]

如果跟踪:
pprint(frame.f_locals)
pdb.set_trace(框架)
return frame.f_locals

用法:

 >>> def screwyFunc():
a = 0
返回2 / a

>>> screwyFunc()

追溯(最近的最后一次调用):
文件< pyshell#62>,第1行,< module>
screwyFunc()
文件< pyshell#55>,第3行,in screwyFunc
返回2 / a
ZeroDivisionError:整数除或模数为零
>>> findlocals('screwyFunc')
{'a':0}


Yesterday I made a simulation using Python. I had a few difficulties with variables and debugging.

Is there any software for Python, which provides a decent debugger?

Related question: What is the best way to debug my Python code?

解决方案

Don't forget about post-mortem debugging! After an exception is thrown, the stack frame with all of the locals is contained within sys.last_traceback. You can do pdb.pm() to go to the stack frame where the exception was thrown then p(retty)p(rint) the locals().

Here is a function that uses this information to extract the local variables from the stack.

def findlocals(search, startframe=None, trace=False):

    from pprint import pprint
    import inspect, pdb

    startframe = startframe or sys.last_traceback
    frames = inspect.getinnerframes(startframe)

    frame = [tb for (tb, _, lineno, fname, _, _) in frames
             if search in (lineno, fname)][0]

    if trace:
        pprint(frame.f_locals)
        pdb.set_trace(frame)
    return frame.f_locals

Usage:

>>> def screwyFunc():
    a = 0
    return 2/a

>>> screwyFunc()

Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    screwyFunc()
  File "<pyshell#55>", line 3, in screwyFunc
    return 2/a
ZeroDivisionError: integer division or modulo by zero
>>> findlocals('screwyFunc')
{'a': 0}

这篇关于Python调试工具的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆