Python代码调试

2022-09-08 12:56:34 浏览数 (1)

一、概述

写好的代码能直接运行的概率非常低,总会在不经意间出现各种各样的BUG,有的BUG很简单,看看错误提示就能修改好。但是有的BUG很复杂,需要一些调试的手段来发现并解决错误

二、print

print是最简单最原始的调试方式

代码语言:javascript复制
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(x)</span>:</span>
    print(<span class="hljs-string">"-----------1"</span>)
    print(x)
    ret = <span class="hljs-number">10</span> / x
    print(<span class="hljs-string">"-----------2"</span>)
    <span class="hljs-keyword">return</span> ret

func(<span class="hljs-number">0</span>)

缺点:将来得删除调试的print,运行结果也会包含一些垃圾信息

三、断言

使用

凡是用print来辅助调试的地方,都可以缓存断言(assert)语句

逻辑

当程序执行到assert语句时,首先计算第一个表达式的值,如果值为真则继续向下运行。否则断言失败,assert语句会抛出AssertionError异常,异常的信息为第二个表达式的值

缺点

如果将所有的print换成assert也好不到哪里去

优点

在启动程序时可以通过命令参数关闭assert

代码

代码语言:javascript复制
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(x)</span>:</span>
    <span class="hljs-keyword">assert</span> x != <span class="hljs-number">0</span>, <span class="hljs-string">"x is zero"</span>
    ret = <span class="hljs-number">10</span> / x
    <span class="hljs-keyword">return</span> ret

func(<span class="hljs-number">0</span>)

四、logging

使用:把所有的print替换成logging也是可以的,和assert相比logging不会抛出异常,把错误输入到文件

代码语言:javascript复制
<span class="hljs-keyword">import</span> logging

<span class="hljs-comment">#配置输出级别</span>
logging.basicConfig(level=logging.ERROR)
<span class="hljs-string">'''
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
'''</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(x)</span>:</span>
    logging.error(<span class="hljs-string">"x = %d"</span>%(x))
    ret = <span class="hljs-number">10</span> / x
    <span class="hljs-keyword">return</span> ret

func(<span class="hljs-number">0</span>)

五、pdb

使用

python的调试器,让程序以单步方式一句一句的运行,可以随时查看运行的状态

以pdb方式调试代码

需要使用黑屏终端: python -m pdb 文件名称

调试命令

n:单步执行代码

p: 变量名

q:退出调试

基本使用

代码语言:javascript复制
a = <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------0"</span>)
b = a   <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------1"</span>)
print(<span class="hljs-string">"-------------------------2"</span>)
c = <span class="hljs-number">2</span>
print(<span class="hljs-string">"-------------------------3"</span>)
print(<span class="hljs-string">"-------------------------4"</span>)
print(<span class="hljs-string">"-------------------------5"</span>)
d = <span class="hljs-number">0</span>
print(<span class="hljs-string">"-------------------------6"</span>)
c / d
print(<span class="hljs-string">"-------------------------7"</span>)
print(<span class="hljs-string">"-------------------------8"</span>)
f = <span class="hljs-number">10</span>
print(<span class="hljs-string">"-------------------------9"</span>)

缺点:麻烦,如果有100000代码,认为错误可能出现在第9000左右,需要运行到9000行才行

自定义开始断点

代码语言:javascript复制
<span class="hljs-keyword">import</span> pdb

a = <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------0"</span>)
b = a   <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------1"</span>)
print(<span class="hljs-string">"-------------------------2"</span>)
c = <span class="hljs-number">2</span>
print(<span class="hljs-string">"-------------------------3"</span>)
print(<span class="hljs-string">"-------------------------4"</span>)
print(<span class="hljs-string">"-------------------------5"</span>)
d = <span class="hljs-number">0</span>
<span class="hljs-comment">#自定义断点</span>
pdb.set_trace()
print(<span class="hljs-string">"-------------------------6"</span>)
c / d
print(<span class="hljs-string">"-------------------------7"</span>)
print(<span class="hljs-string">"-------------------------8"</span>)
f = <span class="hljs-number">10</span>
print(<span class="hljs-string">"-------------------------9"</span>)

六、IDE断点

代码语言:javascript复制
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"**********1"</span>)
    print(<span class="hljs-string">"**********2"</span>)
    print(<span class="hljs-string">"**********3"</span>)
    print(<span class="hljs-string">"**********4"</span>)

a = <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------0"</span>)
b = a   <span class="hljs-number">1</span>
print(<span class="hljs-string">"-------------------------1"</span>)
print(<span class="hljs-string">"-------------------------2"</span>)
c = <span class="hljs-number">2</span>
print(<span class="hljs-string">"-------------------------3"</span>)
print(<span class="hljs-string">"-------------------------4"</span>)
func()
print(<span class="hljs-string">"-------------------------5"</span>)
d = <span class="hljs-number">0</span>
print(<span class="hljs-string">"-------------------------6"</span>)
c / d
print(<span class="hljs-string">"-------------------------7"</span>)
print(<span class="hljs-string">"-------------------------8"</span>)
f = <span class="hljs-number">10</span>
print(<span class="hljs-string">"-------------------------9"</span>)

0 人点赞