翻译:《实用的Python编程》08_03_Debugging
2021-04-11 20:28
639 查看
目录 | 上一节 (8.2 日志) | 下一节 (9 包)
8.3 调试
调试建议
假设程序崩溃了:
bash % python3 blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) AttributeError: 'int' object has no attribute 'append'
那么现在该怎么办呢?
阅读回溯信息
最后一行是程序崩溃的具体原因:
bash % python3 blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) # Cause of the crash AttributeError: 'int' object has no attribute 'append'
不过,回溯信息并不总是那么易于阅读或理解。
专业建议:将整个回溯粘贴到谷歌。
使用交互式解释器(REPL)
执行脚本的 时候,可以使用选项
-i使 Python 保持存活(keep alive)。
bash % python3 -i blah.py Traceback (most recent call last): File "blah.py", line 13, in ? foo() File "blah.py", line 10, in foo bar() File "blah.py", line 7, in bar spam() File "blah.py", 4, in spam line x.append(3) AttributeError: 'int' object has no attribute 'append' >>>
选项
-i可以保留解释器状态。这意味着可以在程序崩溃后查找错误信息。对变量的值和其它状态进行检查。
使用打印进行调试
使用
print()函数进行调试非常常见。
建议:确保使用的是
repr()函数。
def spam(x): print('DEBUG:', repr(x)) ...
repr()函数显示一个值的准确表示,而不是格式良好的输出。
>>> from decimal import Decimal >>> x = Decimal('3.4') # NO `repr` >>> print(x) 3.4 # WITH `repr` >>> print(repr(x)) Decimal('3.4') >>>
Python 的调试器
可以在程序内手动启动调试器(debugger)。
def some_function(): ... breakpoint() # Enter the debugger (Python 3.7+) ...
上述操作会在
breakpoint()调用时启动调试器。
在 Python 的早期版本中,可能会看到下面这样的调试指南:
import pdb ... pdb.set_trace() # Instead of `breakpoint()` ...
(译注:Python 3.7 之后,可以使用内置函数
breakpoint()代替
import pdb; pdb.set_trace())
在调试解释器下运行程序
也可以在调试器下运行整个程序:
bash % python3 -m pdb someprogram.py
上述操作会在第一行语句之前自动进入调试器,允许设置断点和修改配置。
常见的调试器命令:
(Pdb) help # Get help (Pdb) w(here) # Print stack trace (Pdb) d(own) # Move down one stack level (Pdb) u(p) # Move up one stack level (Pdb) b(reak) loc # Set a breakpoint (Pdb) s(tep) # Execute one instruction (Pdb) c(ontinue) # Continue execution (Pdb) l(ist) # List source code (Pdb) a(rgs) # Print args of current function (Pdb) !statement # Execute statement
断点的位置可以用下列任意一种方式进行表示:
(Pdb) b 45 # Line 45 in current file (Pdb) b file.py:45 # Line 34 in file.py (Pdb) b foo # Function foo() in current file (Pdb) b module.foo # Function foo() in a module
练习
练习 8.4:Bugs? 什么是 Bugs?
有 bug,我们就解决 bug(It runs. Ship it!)。
目录 | 上一节 (8.2 日志) | 下一节 (9 包)
相关文章推荐
- 翻译:《实用的Python编程》03_00_Overview
- 翻译:《实用的Python编程》03_01_Script
- 翻译:《实用的Python编程》03_02_More_functions
- 翻译:《实用的Python编程》03_05_Main_module
- 翻译:《实用的Python编程》08_00_Overview
- 翻译:《实用的Python编程》03_06_Design_discussion
- 翻译:《实用的Python编程》06_03_Producers_consumers
- 翻译:《实用的Python编程》08_02_Logging
- 翻译:《实用的Python编程》07_03_Returning_functions
- [翻译]架构师应该知道的97件事_03关键问题可能不是出在技术上
- 翻译03
- 深度学习(08)_RNN-LSTM循环神经网络-03-Tensorflow进阶实现
- 01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之查询缓存
- 深度学习(08)_RNN-LSTM循环神经网络-03-Tensorflow进阶实现
- 08-03-03>pe_xscan 增加对文件类型扩展名隐藏性的检测
- 【翻译】Pro.Silverlight.5.in.CSharp.4th.Edition - 第一章 Silverlight介绍 03
- 翻译:《实用的Python编程》01_02_Hello_world
- 03-limit-memory-usage-es控制聚合内存使用-elasticsearch权威指南翻译
- WordProcessingML的解剖学翻译04--Wordprocessing段落02--属性08--制表符
- Android Develop Training中文翻译03 《Creating an Android Project》