python逐行调试_Python如何实现单步调试

方法一:执行 python -m pdb myscript.py

(Pdb) 会自己主动停在第一行。等待调试,这时你能够看看 帮助

方法二:在所调试程序的开头中:import pdb 并在你的代码行设置断点:pdb.set_trace()

(Pdb) h说明下这几个关键 命令<断点设置(Pdb)b 10 #断点设置在本py的第10行或(Pdb)b ots.py:20 #断点设置到 ots.py第20行删除断点(Pdb)b #查看断点编号(Pdb)cl 2 #删除第2个断点<执行(Pdb)n #单步执行(Pdb)s #细点执行 也就是会下到,方法(Pdb)c #跳到下个断点<查看(Pdb)p param #查看当前 变量值(Pdb)l #查看执行到某处代码(Pdb)a #查看所有栈内变量<假设是在 命令行里的调试为:import pdbdef tt():pdb.set_trace()for i in range(1, 5):print i<<< tt()#这里支持 n p c 而已< (3)tt()(Pdb) n

pdb单步调试方法总结如下:

命令解释

break 或 b 设置断点设置断点

continue 或 c继续执行程序

list 或 l查看当前行的代码段

step 或 s进入函数

return 或 r执行代码直到从当前函数返回

exit 或 q中止并退出

next 或 n执行下一行

pp打印变量的值

a查看全部栈内变量

遇到大型python项目,如何定位问题和监控程序的运行状态是一个程序员必须掌握的技能,今天小编为你带来python程序的单步调试方法,方便易用,简单易记!

首先你需要在所调试程序的开头中:import pdb 并在你的代码行设置断点:pdb.set_trace()

程序开始之后pdb调试界面:

> c:\users\wangchao\workspace\interface_test\testframe.py(253)HTTPInvoke()-> if reqmethod.upper()=="GET": (Pdb) l #执行命令l,会显示出当前代码的上下文,下面的‘->’就是当前即将执行的代码248 def HTTPInvoke(url,requestUri):249 proto,rest=urllib.splittype(url) 250 host,rest =urllib.splithost(rest)251 conn = httplib.HTTPConnection(host) 252 pdb.set_trace()253 -> if reqmethod.upper()=="GET":254 print url255 conn.request(reqmethod.upper(), url,headers=reqHeaders)256 rsps = conn.getresponse()257 if rsps.status==200:258 data = rsps.read()(Pdb) reqmethod.upper() #可以直接输入相关变量名称来查看当前变量的值'GET'(Pdb) n #n就是next的意思就是执行下一行> c:\users\wangchao\workspace\interface_test\testframe.py(254)HTTPInvoke()-> print url(Pdb) b 260 #设置断点Breakpoint 1 at c:\users\wangchao\workspace\interface_test\testframe.py:260(Pdb) n> c:\users\wangchao\workspace\interface_test\testframe.py(254)HTTPInvoke()-> print url(Pdb) http://yue.sogou.com/api/h5/v1/history/recharge/list?pageNo=1&pageSize=1> c:\users\wangchao\workspace\interface_test\testframe.py(255)HTTPInvoke()-> conn.request(reqmethod.upper(), url,headers=reqHeaders)(Pdb) s # 上面通过执行n程序运行到调用函数的地方,使用s可以进入函数内部--Call--> c:\python27\lib\httplib.py(1040)request()-> def request(self, method, url, body=None, headers={}):(Pdb) a # 显示当前所有栈变量的值self = method = GETurl = http://yue.sogou.com/api/h5/v1/history/recharge/list?pageNo=1&pageSize=1body = Noneheaders = {'Connection': 'keep-alive', 'Cookie': 'SUV=004C0C0F6FCA67CB585CD8F53FC5D135; CXID=647977743F187E018526B8ECA587A052; IPLOC=CN1100; OPENID=6D4569C5D00A35876E60A94E34D685AD; pgv_pvi=2641157120; _ga=GA1.2.2141014782.1484890447; ssuid=5496173722; SMYUV=1485154923097202; sgsa_id=sogou.com|1486180882291442; GOTO=Af99046; clientId=291C8B8E05B2647F206981AD04136539; JSESSIONID=aaa_iPLt7BVjahJ1G5GOv; SNUID=FB57E5402F2A672762F7CB303085C13A; ld=uRGSyZllll2YYa4WlllllVAtUeYlllllWTieMkllll9llllljylll5@@@@@@@@@@; LSTMV=316%2C266; LCLKINT=3486; sct=61; ad=jkllllllll2Yg2o@lllllVANab9lllllbDIXhZllllwllllljOxlw@@@@@@@@@@@; SUID=7BC6CA017F430E0A0000000052256039; YYID=AA6E39EBC7D6CC4AA0839B4929E7511C; usid=pp63hL5QOQSxi2gw; sgid=AViadNOrPzjYb8SzAw5wsq5g', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0'}(Pdb) l1035 self.__state = _CS_REQ_SENT1036 else:1037 raise CannotSendHeader()1038 self._send_output(message_body)1039 1040 -> def request(self, method, url, body=None, headers={}):1041 """Send a complete request to the server."""1042 self._send_request(method, url, body, headers)1043 1044 def _set_content_length(self, body, method):1045 # Set the content-length based on the body. If the body is "empty", we(Pdb) r #命令r返回到前面所进入函数的末尾--Return--> c:\python27\lib\httplib.py(1042)request()->None-> self._send_request(method, url, body, headers)(Pdb) l # 可以通过l验证一下当前程序执行的位置1037 raise CannotSendHeader()1038 self._send_output(message_body)1039 1040 def request(self, method, url, body=None, headers={}):1041 """Send a complete request to the server."""1042 -> self._send_request(method, url, body, headers)1043 1044 def _set_content_length(self, body, method):1045 # Set the content-length based on the body. If the body is "empty", we1046 # set Content-Length: 0 for methods that expect a body (RFC 7230,1047 # Section 3.3.2). If the body is set for other methods, we set the(Pdb) r # 再执行r返回到调用该函数的地方> c:\users\wangchao\workspace\interface_test\testframe.py(256)HTTPInvoke()-> rsps = conn.getresponse()(Pdb) l 251 conn = httplib.HTTPConnection(host) 252 pdb.set_trace()253 if reqmethod.upper()=="GET":254 print url255 conn.request(reqmethod.upper(), url,headers=reqHeaders)256 -> rsps = conn.getresponse()257 if rsps.status==200:258 data = rsps.read()259 data = str(data) 260 B conn.close() 261 return data(Pdb) c #执行命令c继续运行程序,直到断点就停留在此位置,上面设置断点的命令“b 260”260表示第多少行> c:\users\wangchao\workspace\interface_test\testframe.py(260)HTTPInvoke()-> conn.close()(Pdb) l255 conn.request(reqmethod.upper(), url,headers=reqHeaders)256 rsps = conn.getresponse()257 if rsps.status==200:258 data = rsps.read()259 data = str(data) 260 B-> conn.close() 261 return data262 elif rsps.status==301 or rsps.status==302:263 headerstr=rsps.getheaders()264 for i in headerstr:265 if i[0].lower()=='location':(Pdb) pp data # pp 打印某个变量的值'{"code":0,"data":{"pageCount":0,"pageList":[],"pageNo":1,"pageSize":1,"totalCount":0},"msg":"\xe6\x88\x90\xe5\x8a\x9f"}'(Pdb)

总结:上面的程序是本人私有,因而不能全部上传,在练习pdb时,建议使用自己的程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值