【问题标题】:How to know what function throws the exception in Python如何知道 Python 中哪个函数引发了异常
【发布时间】:2021-05-22 12:51:29
【问题描述】:

我有一个超过 2000 行的 python 脚本,它通过生产服务器上的以下异常,我无法在本地重现问题来调试它,我不知道它来自哪里。

Exception TypeError: TypeError("argument of type 'NoneType' is not iterable",) in <bound method Popen3.__del__ of <popen2.Popen3 instance at 0x7fccba7b65f0>> ignored
Exception TypeError: TypeError("argument of type 'NoneType' is not iterable",) in <bound method Popen3.__del__ of <popen2.Popen3 instance at 0x7fccba7b62d8>> ignored
Exception TypeError: TypeError("argument of type 'NoneType' is not iterable",) in <bound method Popen3.__del__ of <popen2.Popen3 instance at 0x7fccba824ef0>> ignored
Exception TypeError: TypeError("argument of type 'NoneType' is not iterable",) in <bound method Popen3.__del__ of <popen2.Popen3 instance at 0x7fccba824f80>> ignored

有没有办法让解释器像 Java 那样打印异常的跟踪?能够知道是什么引发了这个异常。

【问题讨论】:

标签: python debugging


【解决方案1】:

最简单的方法是在except 块中使用traceback.print_exc()

try:
    1[0]  # raises
except TypeError:
    traceback.print_exc()
    raise

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多