您好,登錄后才能下訂單哦!
小編給大家分享一下python中debug實(shí)用工具pdb怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
使用介紹
如何添加斷點(diǎn)?
說到 debug,肯定是要添加斷點(diǎn)的,這里有兩種方式添加斷點(diǎn):
在想要斷點(diǎn)代碼后添加 一行
pdb.set_trace()
若是使用這種方式,直接運(yùn)行 Python 文件即可進(jìn)入斷點(diǎn)調(diào)試。
用命令行來添加斷點(diǎn)
b line_number(代碼行數(shù))
若是使用這種方式,需要 python -m pdb xxx.py 來啟動斷點(diǎn)調(diào)試。
常用命令
先簡單介紹一下使用命令,這里不用記住,等用到的時候回來查就行。
1 進(jìn)入命令行Debug模式,python -m pdb xxx.py
2 h: (help)幫助
3 w: (where)打印當(dāng)前執(zhí)行堆棧
4 d: (down)執(zhí)行跳轉(zhuǎn)到在當(dāng)前堆棧的深一層(個人沒覺得有什么用處)
5 u: (up)執(zhí)行跳轉(zhuǎn)到當(dāng)前堆棧的上一層
6 b: (break)添加斷點(diǎn)
b 列出當(dāng)前所有斷點(diǎn),和斷點(diǎn)執(zhí)行到統(tǒng)計(jì)次數(shù)
b line_no:當(dāng)前腳本的line_no行添加斷點(diǎn)
b filename:line_no:腳本filename的line_no行添加斷點(diǎn)
b function:在函數(shù)function的第一條可執(zhí)行語句處添加斷點(diǎn)
7 tbreak: (temporary break)臨時斷點(diǎn)
在第一次執(zhí)行到這個斷點(diǎn)之后,就自動刪除這個斷點(diǎn),用法和b一樣
8 cl: (clear)清除斷點(diǎn)
cl 清除所有斷點(diǎn)
cl bpnumber1 bpnumber2... 清除斷點(diǎn)號為bpnumber1,bpnumber2...的斷點(diǎn)
cl lineno 清除當(dāng)前腳本lineno行的斷點(diǎn)
cl filename:line_no 清除腳本filename的line_no行的斷點(diǎn)
9 disable:停用斷點(diǎn),參數(shù)為bpnumber,和cl的區(qū)別是,斷點(diǎn)依然存在,只是不啟用
10 enable:激活斷點(diǎn),參數(shù)為bpnumber
11 s: (step)執(zhí)行下一條命令
如果本句是函數(shù)調(diào)用,則s會執(zhí)行到函數(shù)的第一句
12 n: (next)執(zhí)行下一條語句
如果本句是函數(shù)調(diào)用,則執(zhí)行函數(shù),接著執(zhí)行當(dāng)前執(zhí)行語句的下一條。
13 r: (return)執(zhí)行當(dāng)前運(yùn)行函數(shù)到結(jié)束
14 c: (continue)繼續(xù)執(zhí)行,直到遇到下一條斷點(diǎn)
15 l: (list)列出源碼
l 列出當(dāng)前執(zhí)行語句周圍11條代碼
l first 列出first行周圍11條代碼
l first second 列出first--second范圍的代碼,如果second<first,second將被解析為行數(shù)
16 a: (args)列出當(dāng)前執(zhí)行函數(shù)的函數(shù)
17 p expression:(print)輸出expression的值
18 pp expression:好看一點(diǎn)的p expression
19 run:重新啟動debug,相當(dāng)于restart
20 q:(quit)退出debug
21 j lineno:(jump)設(shè)置下條執(zhí)行的語句函數(shù)
只能在堆棧的最底層跳轉(zhuǎn),向后重新執(zhí)行,向前可直接執(zhí)行到行號
22)unt:(until)執(zhí)行到下一行(跳出循環(huán)),或者當(dāng)前堆棧結(jié)束
23)condition bpnumber conditon,給斷點(diǎn)設(shè)置條件,當(dāng)參數(shù)condition返回True的時候bpnumber斷點(diǎn)有效,否則bpnumber斷點(diǎn)無效
舉個簡單的栗子
為了驗(yàn)證一下 pdb 的用法,我寫了個簡單的 Python 代碼,如下:
__author__ = 'zone' __gzh__ = '公號:zone7' import pdb class MyScrapy: urls = [] def start_url(self, urls): pdb.set_trace() for url in urls: print(url) self.urls.append(url) def parse(self): pdb.set_trace() for url in self.urls: result = self.request_something(url) def request_something(self, url): print('requesting...') data = '''<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html>''' return data scrapy= MyScrapy() scrapy.start_url(["http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", "http://www.zone7.cn", ]) scrapy.parse()
運(yùn)行實(shí)例:(這里為了方便大家閱讀,我添加了中文注釋,實(shí)際運(yùn)行時不會有注釋的)
D:workenvScriptspython.exe D:/work_test/test/pdb_test/pdb_test.py > d:work_test estpdb_testpdb_test.py(11)start_url() -> for url in urls: (Pdb) n 注釋:n(next)執(zhí)行下一步 > d:work_test estpdb_testpdb_test.py(12)start_url() -> print(url) (Pdb) l 注釋: l(list)列出當(dāng)前代碼 7 urls = [] 8 9 def start_url(self, urls): 10 pdb.set_trace() 11 for url in urls: 12 -> print(url) 13 self.urls.append(url) 14 15 def parse(self): 16 pdb.set_trace() 17 for url in self.urls: (Pdb) c 注釋:c(continue),繼續(xù)執(zhí)行,知道遇到下一個斷點(diǎn) http://www.zone7.cn http://www.zone7.cn http://www.zone7.cn http://www.zone7.cn > d:work_test estpdb_testpdb_test.py(17)parse() -> for url in self.urls: (Pdb) n 注釋:n(next)執(zhí)行下一步 > d:work_test estpdb_testpdb_test.py(18)parse() -> result = self.request_something(url) (Pdb) l 注釋: l(list)列出當(dāng)前代碼 13 self.urls.append(url) 14 15 def parse(self): 16 pdb.set_trace() 17 for url in self.urls: 18 -> result = self.request_something(url) 19 20 def request_something(self, url): 21 print('requesting...') 22 data = '''<!DOCTYPE html> 23 <html lang="en"> (Pdb) s 注釋: s(step)這里是進(jìn)入 request_something() 函數(shù)的意思 --Call-- > d:work_test estpdb_testpdb_test.py(20)request_something() -> def request_something(self, url): (Pdb) n 注釋:n(next)執(zhí)行下一步 > d:work_test estpdb_testpdb_test.py(21)request_something() -> print('requesting...') (Pdb) l 注釋: l(list)列出當(dāng)前代碼 16 pdb.set_trace() 17 for url in self.urls: 18 result = self.request_something(url) 19 20 def request_something(self, url): 21 -> print('requesting...') 22 data = '''<!DOCTYPE html> 23 <html lang="en"> 24 <head> 25 <meta charset="UTF-8"> 26 <title>Title</title> (Pdb) p url 注釋:p(print)打印出 url 變量的數(shù)據(jù) 'http://www.zone7.cn' (Pdb) n 注釋:n(next)執(zhí)行下一步 requesting... > d:work_test estpdb_testpdb_test.py(31)request_something() -> </html>''' (Pdb) p data 注釋:p(print)打印出指定變量的數(shù)據(jù),這里由于賦值還沒完成,所以報錯 *** NameError: name 'data' is not defined (Pdb) n 注釋:n(next)執(zhí)行下一步 > d:work_test estpdb_testpdb_test.py(32)request_something() -> return data (Pdb) p data 注釋:p(print)打印出指定變量的數(shù)據(jù) '<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html>' (Pdb) q 注釋:q(quit)退出
以上是“python中debug實(shí)用工具pdb怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。