使用介紹
如何添加斷點(diǎn)?
說(shuō)到 debug,肯定是要添加斷點(diǎn)的,這里有兩種方式添加斷點(diǎn):
在想要斷點(diǎn)代碼后添加 一行
pdb.set_trace()
若是使用這種方式,直接運(yùn)行 Python 文件即可進(jìn)入斷點(diǎn)調(diào)試。
用命令行來(lái)添加斷點(diǎn)
b line_number
若是使用這種方式,需要 python -m pdb xxx.py 來(lái)啟動(dòng)斷點(diǎn)調(diào)試。
常用命令
先簡(jiǎn)單介紹一下使用命令,這里不用記住,等用到的時(shí)候回來(lái)查就行。
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)前堆棧的深一層(個(gè)人沒覺得有什么用處)
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í)行語(yǔ)句處添加斷點(diǎn)
7 tbreak:(temporary break)臨時(shí)斷點(diǎn)
8 cl:(clear)清除斷點(diǎn)
cl 清除所有斷點(diǎn)
cl bpnumber1 bpnumber2... 清除斷點(diǎn)號(hào)為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會(huì)執(zhí)行到函數(shù)的第一句
12 n:(next)執(zhí)行下一條語(yǔ)句
如果本句是函數(shù)調(diào)用,則執(zhí)行函數(shù),接著執(zhí)行當(dāng)前執(zhí)行語(yǔ)句的下一條。
13 r:(return)執(zhí)行當(dāng)前運(yùn)行函數(shù)到結(jié)束
14 c:(continue)繼續(xù)執(zhí)行,直到遇到下一條斷點(diǎn)
15 l:(list)列出源碼
l 列出當(dāng)前執(zhí)行語(yǔ)句周圍11條代碼
l first 列出first行周圍11條代碼
l first second 列出first--second范圍的代碼,如果second
16 a:(args)列出當(dāng)前執(zhí)行函數(shù)的函數(shù)
17 p expression:(print)輸出expression的值
18 pp expression:好看一點(diǎn)的p expression
19 run:重新啟動(dòng)debug,相當(dāng)于restart
20 q:(quit)退出debug
21 j lineno:(jump)設(shè)置下條執(zhí)行的語(yǔ)句函數(shù)
只能在堆棧的最底層跳轉(zhuǎn),向后重新執(zhí)行,向前可直接執(zhí)行到行號(hào)
22)unt:(until)執(zhí)行到下一行(跳出循環(huán)),或者當(dāng)前堆棧結(jié)束
23)condition bpnumber conditon,給斷點(diǎn)設(shè)置條件,當(dāng)參數(shù)condition返回True的時(shí)候bpnumber斷點(diǎn)有效,否則bpnumber斷點(diǎn)無(wú)效
舉個(gè)簡(jiǎn)單的栗子
為了驗(yàn)證一下 pdb 的用法,我寫了個(gè)簡(jiǎn)單的 Python 代碼,如下:
運(yùn)行實(shí)例:(這里為了方便大家閱讀,我添加了中文注釋,實(shí)際運(yùn)行時(shí)不會(huì)有注釋的)
D:\work\venv\Scripts\python.exe D:/work_test/test/pdb_test/pdb_test.py
> d:\work_test\test\pdb_test\pdb_test.py(11)start_url()
-> for url in urls:
(Pdb) n? 注釋:n(next)執(zhí)行下一步
> d:\work_test\test\pdb_test\pdb_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í)行,知道遇到下一個(gè)斷點(diǎn)
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
http://www.zone7.cn
> d:\work_test\test\pdb_test\pdb_test.py(17)parse()
-> for url in self.urls:
(Pdb) n? 注釋:n(next)執(zhí)行下一步
> d:\work_test\test\pdb_test\pdb_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 = '''
23? ? ?
(Pdb) s 注釋: s(step)這里是進(jìn)入 request_something() 函數(shù)的意思
--Call--
> d:\work_test\test\pdb_test\pdb_test.py(20)request_something()
-> def request_something(self, url):
(Pdb) n? 注釋:n(next)執(zhí)行下一步
> d:\work_test\test\pdb_test\pdb_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 = '''
23? ? ?
24? ? ?
25? ? ? ? ?
26? ? ? ? ?
(Pdb) p url? 注釋:p(print)打印出 url 變量的數(shù)據(jù)
'http://www.zone7.cn'
(Pdb) n? 注釋:n(next)執(zhí)行下一步
requesting...
> d:\work_test\test\pdb_test\pdb_test.py(31)request_something()
-> '''
(Pdb) p data? 注釋:p(print)打印出指定變量的數(shù)據(jù),這里由于賦值還沒完成,所以報(bào)錯(cuò)
*** NameError: name 'data' is not defined
(Pdb) n? 注釋:n(next)執(zhí)行下一步
> d:\work_test\test\pdb_test\pdb_test.py(32)request_something()
-> return data
(Pdb) p data? 注釋:p(print)打印出指定變量的數(shù)據(jù)
'\n\n\n? ? \n? ?
(Pdb) q? 注釋:q(quit)退出
總結(jié)
按照上面的例子一套下來(lái),基本的用法就可以學(xué)會(huì)了,關(guān)鍵還是得自己多實(shí)踐,今天就寫到這,還想寫一篇關(guān)于性能調(diào)試的文章,不知道這兩天有沒有時(shí)間了
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
