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