http://docs.python.org/library/pdb.htmlhtml
http://magustest.com/blog/computer/use-pdb-debug-python/python
用pdb調試有多種方式可選:函數
1. 命令行啓動目標程序,加上-m參數,這樣調用myscript.py的話斷點就是程序的執行第一行以前
python -m pdb myscript.pyui
2. 在Python交互環境中啓用調試
>>> import pdb
>>> import mymodule
>>> pdb.run(‘mymodule.test()’)spa
3. 比較經常使用的,就是在程序中間插入一段程序,相對於在通常IDE裏面打上斷點而後啓動debug,不過這種方式是hardcode的命令行
if __name__ == "__main__": a = 1 import pdb pdb.set_trace() b = 2 c = a + b print (c)
而後正常運行腳本,到了pdb.set_trace()那就會定下來,就能夠看到調試的提示符(Pdb)了debug
經常使用的調試命令調試
(Pdb) l
497 pdb.set_trace()
498 base_data = {}
499 new_data = {}
500 try:
501 execfile(base_file_name,{},base_data)
502 -> execfile(new_file_name,{},new_data)
503 except:
504 logger.writeLog(「error! load result log error!」)
505 print 「load cmp logs error!」
506 raise Exception, 「load cmp logs error!」
507code
(Pdb) b 504
Breakpoint 4 at /home/jchen/regression/regressionLogCMP.py:504htm
(Pdb) condition 4 a==3
(Pdb) b
Num Type Disp Enb Where
4 breakpoint keep yes at /home/jchen/regression/regressionLogCMP.py:504
stop only if a==3
(Pdb) cl
Clear all breaks? y
(Pdb) disable 3
(Pdb) b
Num Type Disp Enb Where
3 breakpoint keep no at /home/jchen/regression/regressionLogCMP.py:505
(Pdb) j 497
> /home/jchen/regression/regressionLogCMP.py(497)compareLog()
-> pdb.set_trace()
(Pdb) a
_logger =
_base = ./base/MRM-8137.log
_new = ./new/MRM-8137.log
_caseid = 5550001
_toStepNum = 10
_cmpMap = {‘_bcmpbinarylog’: ‘True’, ‘_bcmpLog’: ‘True’, ‘_bcmpresp’: ‘True’}
(Pdb) p _new
u’./new/MRM-8137.log’
發如今命令行下調試程序也是一件挺有意思的事情,記錄下來分享一下