Try:
python -m pdb test.py arg1 arg2
Running python -m pdb
runs pdb
as a script. If test.py
is somewhere in your path rather than your current working directory, this can be a helpful substitute:
python -m pdb "$(which test.py)" arg1 arg2
單步執行代碼,通過命令 python -m pdb xxx.py 啟動腳本,進入單步執行模式
pdb命令行:
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斷點無效
注意:
1:直接輸入Enter,會執行上一條命令;
2:輸入PDB不認識的命令,PDB會把他當做Python語句在當前環境下執行;
REF
https://www.cnblogs.com/xiaohai2003ly/p/8529472.html
https://www.cnblogs.com/bonelee/p/11344053.html