码迷,mamicode.com
首页 > 其他好文 > 详细

15 调试

时间:2017-12-01 23:23:55      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:and   ace   ++   import   命令   cal   turn   ipy   home   

1. pdb

pdb是基于命令行的调试工具,非常类似gnu的gdb(调试c/c++)。

def getAverage(a,b):
    result = a+b
    print("result is %s"%result)
    return result

a = 10
b = 20
c = a+b
ret = getAverage(a,b)
print(ret)

 

 

2.执行时调试

程序启动,停止在第一行等待单步调试。

python -m pdb some.py

     

技术分享图片

 

 

(Pdb) l          #list显示当前的代码
  1  ->    def getAverage(a,b):
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6      a = 10
  7      b = 20
  8      c = a+b
  9      ret = getAverage(a,b)
 10      print(ret)
[EOF]
(Pdb) n              #next 向下执行1行代码
> /home/python/python06/01-调试.py(6)<module>()
-> a = 10
(Pdb) l
  1      def getAverage(a,b):
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6  ->    a = 10
  7      b = 20
  8      c = a+b
  9      ret = getAverage(a,b)
 10      print(ret)
[EOF]
(Pdb) c           #继续执行代码
result is 30
30
The program finished and will be restarted
> /home/python/python06/01-调试.py(1)<module>()
-> def getAverage(a,b):
(Pdb) l
  1  ->    def getAverage(a,b):
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6      a = 10
  7      b = 20
  8      c = a+b
  9      ret = getAverage(a,b)
 10      print(ret)
[EOF]
(Pdb) n
> /home/python/python06/01-调试.py(6)<module>()
-> a = 10
(Pdb) l
  1      def getAverage(a,b):
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6  ->    a = 10
  7      b = 20
  8      c = a+b
  9      ret = getAverage(a,b)
 10      print(ret)
[EOF]

(Pdb) b 9             #第9行添加断点
Breakpoint 1 at /home/python/python06/01-调试.py:9

(Pdb) n
> /home/python/python06/01-调试.py(7)<module>()
-> b = 20
(Pdb) l
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6      a = 10
  7  ->    b = 20
  8      c = a+b
  9 B    ret = getAverage(a,b)
 10      print(ret)
[EOF]
(Pdb) c             
> /home/python/python06/01-调试.py(9)<module>()
-> ret = getAverage(a,b)
(Pdb) l
  4          return result
  5      
  6      a = 10
  7      b = 20
  8      c = a+b
  9 B->    ret = getAverage(a,b)
 10      print(ret)
[EOF]

(Pdb) p a
10
(Pdb) s
--Call--
> /home/python/python06/01-调试.py(1)getAverage()
-> def getAverage(a,b):
(Pdb) l
  1  ->    def getAverage(a,b):
  2          result = a+b
  3          print("result is %s"%result)
  4          return result
  5      
  6      a = 10
  7      b = 20
  8      c = a+b
  9 B    ret = getAverage(a,b)
 10      print(ret)
[EOF]
(Pdb) a
a = 10
b = 20
(Pdb) q      #退出调试

 

3.交互调试

进入python或ipython解释器

import pdb
pdb.run(testfun(args)) #此时会打开pdb调试,注意:先使用s跳转到这个testfun函数中,然后就可以使用l看到代码了

 

 

4.程序里埋点

当程序执行到pdb.set_trace() 位置时停下来调试

代码上下文
...

import pdb 
pdb.set_trace() 

...

 

 

5.日志调试

   大公司只能用日志调试

6.集成开发环境IDE

技术分享图片

 

15 调试

标签:and   ace   ++   import   命令   cal   turn   ipy   home   

原文地址:http://www.cnblogs.com/venicid/p/7944662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!