新聞中心
pdb為python程序?qū)崿F(xiàn)了一個(gè)交互式調(diào)試環(huán)境。它包括一些特性,可以暫停程序,查看變量值,以及逐步監(jiān)視程序執(zhí)行,從而能了解程序具體做了什么,并查找邏輯中存在的bug。

下面結(jié)合具體的實(shí)例講述如何使用 pdb 進(jìn)行調(diào)試。
清單 1. 測試代碼示例
import pdb
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print final
開始調(diào)試:直接運(yùn)行腳本,會停留在 pdb.set_trace() 處,選擇 n+enter 可以執(zhí)行當(dāng)前的 statement。在第一次按下了 n+enter 之后可以直接按 enter 表示重復(fù)執(zhí)行上一條 debug 命令。
清單 2. 利用 pdb 調(diào)試
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb)
> /root/epdb1.py(6)?()
-> final = a + b + c
(Pdb) list
1 import pdb
2 a = "aaa"
3 pdb.set_trace()
4 b = "bbb"
5 c = "ccc"
6 -> final = a + b + c
7 print final
[EOF]
(Pdb)
[EOF]
(Pdb) n
> /root/epdb1.py(7)?()
-> print final
(Pdb)
退出 debug:使用 quit 或者 q 可以退出當(dāng)前的 debug,但是 quit 會以一種非常粗魯?shù)姆绞酵顺龀绦?,其結(jié)果是直接 crash。
清單 3. 退出 debug
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) q
Traceback (most recent call last):
File "epdb1.py", line 5, in ?
c = "ccc"
File "epdb1.py", line 5, in ?
c = "ccc"
File "/usr/lib64/python2.4/bdb.py", line 48, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib64/python2.4/bdb.py", line 67, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
打印變量的值:如果需要在調(diào)試過程中打印變量的值,可以直接使用 p 加上變量名,但是需要注意的是打印僅僅在當(dāng)前的 statement 已經(jīng)被執(zhí)行了之后才能看到具體的值,否則會報(bào) NameError: 錯(cuò)誤。
清單 4. debug 過程中打印變量
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) p b
'bbb'
(Pdb)
'bbb'
(Pdb) n
> /root/epdb1.py(6)?()
-> final = a + b + c
(Pdb) p c
'ccc'
(Pdb) p final
*** NameError:
(Pdb) n
> /root/epdb1.py(7)?()
-> print final
(Pdb) p final
'aaabbbccc'
(Pdb)
使用 c 可以停止當(dāng)前的 debug 使程序繼續(xù)執(zhí)行。如果在下面的程序中繼續(xù)有 set_statement() 的申明,則又會重新進(jìn)入到 debug 的狀態(tài),讀者可以在代碼 print final 之前再加上 set_trace() 驗(yàn)證。
清單 5. 停止 debug 繼續(xù)執(zhí)行程序
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) n
> /root/epdb1.py(5)?()
-> c = "ccc"
(Pdb) c
aaabbbccc
顯示代碼:在 debug 的時(shí)候不一定能記住當(dāng)前的代碼塊,如要要查看具體的代碼塊,則可以通過使用 list 或者 l 命令顯示。list 會用箭頭 -> 指向當(dāng)前 debug 的語句。
清單 6. debug 過程中顯示代碼
[root@rcc-pok-idg-2255 ~]# python epdb1.py
> /root/epdb1.py(4)?()
-> b = "bbb"
(Pdb) list
1 import pdb
2 a = "aaa"
3 pdb.set_trace()
4 -> b = "bbb"
5 c = "ccc"
6 final = a + b + c
7 pdb.set_trace()
8 print final
[EOF]
(Pdb) c
> /root/epdb1.py(8)?()
-> print final
(Pdb) list
3 pdb.set_trace()
4 b = "bbb"
5 c = "ccc"
6 final = a + b + c
7 pdb.set_trace()
8 -> print final
[EOF]
(Pdb)
在使用函數(shù)的情況下進(jìn)行 debug
清單 7. 使用函數(shù)的例子
import pdb
def combine(s1,s2): # define subroutine combine, which...
s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
s3 = '"' + s3 +'"' # encloses it in double quotes,...
return s3 # and returns it.
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = combine(a,b)
print final
如果直接使用 n 進(jìn)行 debug 則到 final=combine(a,b) 這句的時(shí)候會將其當(dāng)做普通的賦值語句處理,進(jìn)入到 print final。如果想要對函數(shù)進(jìn)行 debug 如何處理呢 ? 可以直接使用 s 進(jìn)入函數(shù)塊。函數(shù)里面的單步調(diào)試與上面的介紹類似。如果不想在函數(shù)里單步調(diào)試可以在斷點(diǎn)處直接按 r 退出到調(diào)用的地方。
清單 8. 對函數(shù)進(jìn)行 debug
[root@rcc-pok-idg-2255 ~]# python epdb2.py
> /root/epdb2.py(10)?()
-> b = "bbb"
(Pdb) n
> /root/epdb2.py(11)?()
-> c = "ccc"
(Pdb) n
> /root/epdb2.py(12)?()
-> final = combine(a,b)
(Pdb) s
--Call--
> /root/epdb2.py(3)combine()
-> def combine(s1,s2): # define subroutine combine, which...
(Pdb) n
> /root/epdb2.py(4)combine()
-> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
(Pdb) list
1 import pdb
2
3 def combine(s1,s2): # define subroutine combine, which...
4 -> s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
5 s3 = '"' + s3 +'"' # encloses it in double quotes,...
6 return s3 # and returns it.
7
8 a = "aaa"
9 pdb.set_trace()
10 b = "bbb"
11 c = "ccc"
(Pdb) n
> /root/epdb2.py(5)combine()
-> s3 = '"' + s3 +'"' # encloses it in double quotes,...
(Pdb) n
> /root/epdb2.py(6)combine()
-> return s3 # and returns it.
(Pdb) n
--Return--
> /root/epdb2.py(6)combine()->'"aaabbbaaa"'
-> return s3 # and returns it.
(Pdb) n
> /root/epdb2.py(13)?()
-> print final
(Pdb)
在調(diào)試的時(shí)候動(dòng)態(tài)改變值 。在調(diào)試的時(shí)候可以動(dòng)態(tài)改變變量的值,具體如下實(shí)例。需要注意的是下面有個(gè)錯(cuò)誤,原因是 b 已經(jīng)被賦值了,如果想重新改變 b 的賦值,則應(yīng)該使用! B。
清單 9. 在調(diào)試的時(shí)候動(dòng)態(tài)改變值
[root@rcc-pok-idg-2255 ~]# python epdb2.py
> /root/epdb2.py(10)?()
-> b = "bbb"
(Pdb) var = "1234"
(Pdb) b = "avfe"
*** The specified object '= "avfe"' is not a function
or was not found along sys.path.
(Pdb) !b="afdfd"
(Pdb)
pdb 調(diào)試有個(gè)明顯的缺陷就是對于多線程,遠(yuǎn)程調(diào)試等支持得不夠好,同時(shí)沒有較為直觀的界面顯示,不太適合大型的 python 項(xiàng)目。而在較大的 python 項(xiàng)目中,這些調(diào)試需求比較常見,因此需要使用更為高級的調(diào)試工具。接下來將介紹 PyCharm IDE 的調(diào)試方法 .
文章名稱:使用pdb進(jìn)行python代碼調(diào)試
本文網(wǎng)址:http://m.fisionsoft.com.cn/article/ccicogh.html


咨詢
建站咨詢
