python之錯誤調試


  無論誰寫的程序,必定會存在bug,解決bug需要我們去調試程序。於是乎,在Python中,就會好幾種調試手段,如print、assert、logging、pdb、pdb.set_trace()

  一、使用print()函數直接打印

>>> def foo(s):
...     n = int(s)
...     print(n)
...     return 10 / n
...
>>> def main():
...     foo('0')
...
>>> main()
0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in main
  File "<stdin>", line 4, in foo
ZeroDivisionError: division by zero

  我們可以在認為可能出錯的地方打印變量,但這有很大的弊端,因為打印的代碼沒有實際功能,都是垃圾信息。而且print最后還得刪除,所以第二種方法是用assert替代print

  二、使用斷言assert

>>> def foo(s):
...     n = int(s)
...     assert n != 0,'n的值是0!'
...     return 10 / n
...
>>> def main():
...     foo('0')
...
>>> main()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in main
  File "<stdin>", line 3, in foo
AssertionError: n的值是0!

  assert的意思,當后面的表達式為False時,就會拋出AssertionError,如果為True,什么都不做,直接到下一行。assert有一大特性:在啟動python解釋器的時候可以使用-O參數來關閉assert(大寫的o)

PS E:\Python3.6.3\workspace> python -O err_assert.py
Traceback (most recent call last):
  File "err_assert.py", line 9, in <module>
    main()
  File "err_assert.py", line 7, in main
    foo('0')
  File "err_assert.py", line 4, in foo
    return 10 / n
ZeroDivisionError: division by zer

  三、使用logging

import logging
logging.basicConfig(level=logging.INFO)
s = '0'
n = int(s)
logging.info('n=%d' % n)
print(10/n)

#執行結果
PS E:\Python3.6.3\workspace> python err_logginginfo.py
INFO:root:n=0
Traceback (most recent call last):
  File "err_logginginfo.py", line 6, in <module>
    print(10/n)
ZeroDivisionError: division by zero

  使用logging不會拋出錯誤,直接輸出到文件中。logging可以允許你指定記錄信息的級別,級別由低到高分別是debug、info、warning、error、CRITICAL等級別,當定義高級別的時候,低級別的信息不會輸出,這是把日志信息輸出到控制台console,我們還可以通過設置把日志輸出到文件中

  四、使用python的調試器pdb

  可以讓程序以單步方式執行,方便我們隨時查看運行狀態

  新建程序err_pdb.py

s = '0'
n = int(s)
print(10 / n)

  然后以pdb模式啟動

PS E:\Python3.6.3\workspace> python -m pdb err_pdb.py
> e:\python3.6.3\workspace\err_pdb.py(1)<module>()
-> s = '0'
(Pdb) l
  1  -> s = '0'
  2     n = int(s)
  3     print(10 / n)
[EOF]
(Pdb) n
> e:\python3.6.3\workspace\err_pdb.py(2)<module>()
-> n = int(s)
(Pdb) p s
'0'
(Pdb) p n
*** NameError: name 'n' is not defined
(Pdb) n
> e:\python3.6.3\workspace\err_pdb.py(3)<module>()
-> print(10 / n)
(Pdb) p n
0
(Pdb) p s
'0'
(Pdb) n
ZeroDivisionError: division by zero
> e:\python3.6.3\workspace\err_pdb.py(3)<module>()
-> print(10 / n)
(Pdb) n
--Return--
> e:\python3.6.3\workspace\err_pdb.py(3)<module>()->None
-> print(10 / n)
(Pdb) n
ZeroDivisionError: division by zero
> <string>(1)<module>()->None
(Pdb) n
--Return--
> <string>(1)<module>()->None
(Pdb) n
Traceback (most recent call last):
  File "E:\Python3.6.3\lib\pdb.py", line 1667, in main
    pdb._runscript(mainpyfile)
  File "E:\Python3.6.3\lib\pdb.py", line 1548, in _runscript
    self.run(statement)
  File "E:\Python3.6.3\lib\bdb.py", line 431, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "e:\python3.6.3\workspace\err_pdb.py", line 3, in <module>
    print(10 / n)
ZeroDivisionError: division by zero
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> e:\python3.6.3\workspace\err_pdb.py(3)<module>()->None
-> print(10 / n)
(Pdb) q
Post mortem debugger finished. The err_pdb.py will be restarted
> e:\python3.6.3\workspace\err_pdb.py(1)<module>()
-> s = '0'
(Pdb) n
> e:\python3.6.3\workspace\err_pdb.py(2)<module>()
-> n = int(s)
(Pdb) q
PS E:\Python3.6.3\workspace>
小寫字母l,可以列出所有要執行的代碼;

n 命令表示單步執行代碼;

p 后面加上變量名,可以隨時查看變量的值;

在pdb模式中,對於還沒有單步執行到的代碼,相關的變量的變更是無效的;

q 命令退出當前調試,進入重新從頭開始調試,再次輸入q,就會推出調試程序。

這種方式的調試,有一個弊端,就是只能一步一步的執行下去,如果程序有很多行,豈不是累死。

  五、使用pdb.set_trace()

#err_pdb.py
import pdb

s = '0'
n = int(s)
pdb.set_trace() #程序運行到這里會自動停止,等待命令
print(10 / n)

  我們可以使用l、c、n、p、q等命令來控制和查看程序

PS E:\Python3.6.3\workspace> python err_pdb.py
> e:\python3.6.3\workspace\err_pdb.py(7)<module>()
-> print(10 / n)
(Pdb) p s
'0'
(Pdb) l
  2     import pdb
  3
  4     s = '0'
  5     n = int(s)
  6     pdb.set_trace() #程序運行到這里會自動停止,等待命令
  7  -> print(10 / n)
[EOF]
(Pdb) n
ZeroDivisionError: division by zero
> e:\python3.6.3\workspace\err_pdb.py(7)<module>()
-> print(10 / n)
(Pdb) c
Traceback (most recent call last):
  File "err_pdb.py", line 7, in <module>
    print(10 / n)
ZeroDivisionError: division by zero

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM