python pdb調試以及sublime3快捷鍵設置
pdb調試
如果對gdb比較熟悉的話,pdb就很容易上手。以一個demo快速了解常用的調試命令。
def test(a):
while True:
if a > 10:
break
a += 1
return a
if __name__ == '__main__':
test(1)
- python -m pdb test.py進入調試環境
- b test
在test函數處設置斷點,斷點號為1
(Pdb) b test
Breakpoint 1 at f:\python\pdb\test.py:1
- b 2
在第二行設置斷點,斷點號為2
(Pdb) b 2
Breakpoint 2 at f:\python\pdb\test.py:2
- condition 2 a7
在2號斷點處設置條件 a7 - b
顯示所有斷點信息
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at f:\python\pdb\test.py:1
2 breakpoint keep yes at f:\python\pdb\test.py:2
stop only if a==7
- cl 1
去除1號斷點,只有cl刪除所有斷點
(Pdb) cl 1
Deleted breakpoint 1
- n
單步跟蹤,不進入函數
(Pdb) n
> f:\python\pdb\test.py(8)<module>()
-> if __name__ == '__main__':
(Pdb) n
> f:\python\pdb\test.py(9)<module>()
-> test(1)
- s
單步跟蹤,進入函數
(Pdb) s
--Call--
> f:\python\pdb\test.py(1)test()
-> def test(a):
- c
繼續運行在 a==7 條件斷點處停止 - p a
此時,打印a的值為7
(Pdb) c
> f:\python\pdb\test.py(2)test()
-> while True:
(Pdb) p a
7
- a
打印所在函數參數
(Pdb) a
a = 7
- l
查看運行到某處代碼
(Pdb) l
1 def test(a):
2 B-> while True:
3 if a > 10:
4 break
5 a += 1
6 return a
7 if __name__ == '__main__':
8 test(1)
[EOF]
- quit
退出
sublime設置
sublime設置快捷鍵F5為運行,Ctrl+F5調試。就會對python調試方便很多。
- Package Control中下載SublimeREPL(Read-Eval-Print-Loop)
- Preferneces -> Key Bingdings-User進行設置
[
{
"keys": [
"f5"
],
"caption": "SublimeREPL: Python - RUN current file",
"command": "run_existing_window_command",
"args": {
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
},
{
"keys": [
"ctrl+f5"
],
"caption": "SublimeREPL: Python - PDB current file",
"command": "run_existing_window_command",
"args":
{
"id": "repl_python_pdb",
"file": "config/Python/Main.sublime-menu"
}
}
]