python - 如何知道 Python 中哪个函数引发了异常
问题描述
我有一个超过 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 一样打印异常的跟踪?能够知道是什么引发了这个异常。
解决方案
最简单的方法是traceback.print_exc()
在except
块中使用:
try:
1[0] # raises
except TypeError:
traceback.print_exc()
raise
推荐阅读
- xamarin.forms - Xamarin.Forms:带有 Sharnado.presentation.forms 的选项卡不响应点击
- pycharm - IDE重启后如何保存代码折叠?
- machine-learning - 如何在 LightGBM 中实现与二进制目标具有相同行为的自定义 logloss?
- php - 使用 PHP 安全加密 MySQL 数据库中的静态客户详细信息
- php - 即使我的 php 版本是 7.2.1,在 Laravel 6 composer update 中也出现 php 版本错误
- mysql - MySQL 8.0.12:加载数据 INFILE 给出错误 1290 secure_file_priv
- python - 为什么不调用“解析”方法?
- bigcommerce - 插入脚本时获取“您没有访问端点所需的范围”
- html - '文本装饰线:下划线'不起作用
- python - 如何在安装 mysql-connector 后修复“ImportError:没有名为 mysql.connector 的模块”?