【脚本语言系列】关于Python基础知识调试,你需要知道的事

如何使用调试

  • 从命令行运行
# -*- coding:utf-8 -*-
import pdb

def do_sth():
    pdb.set_trace()
    return "I don't have time"

print do_sth()
> <ipython-input-4-0922ee015237>(6)do_sth()
-> return "I don't have time"
(Pdb) n
--Return--
> <ipython-input-4-0922ee015237>(6)do_sth()->"I don't have time"
-> return "I don't have time"
(Pdb) s #exec the cur line
--Call--
> c:\python27\lib\site-packages\ipykernel\iostream.py(342)write()
-> def write(self, string):
(Pdb) a #print the parameter list
self = <ipykernel.iostream.OutStream object at 0x00000000036BD7F0>
string = I don't have time
(Pdb) w #pritn the function context
  c:\python27\lib\runpy.py(174)_run_module_as_main()
-> "__main__", fname, loader, pkg_name)
  c:\python27\lib\runpy.py(72)_run_code()
-> exec code in run_globals
  c:\python27\lib\site-packages\ipykernel_launcher.py(16)<module>()
-> app.launch_new_instance()
  c:\python27\lib\site-packages\traitlets\config\application.py(658)launch_instance()
-> app.start()
  c:\python27\lib\site-packages\ipykernel\kernelapp.py(477)start()
-> ioloop.IOLoop.instance().start()
  c:\python27\lib\site-packages\zmq\eventloop\ioloop.py(177)start()
-> super(ZMQIOLoop, self).start()
  c:\python27\lib\site-packages\tornado\ioloop.py(887)start()
-> handler_func(fd_obj, events)
  c:\python27\lib\site-packages\tornado\stack_context.py(275)null_wrapper()
-> return fn(*args, **kwargs)
  c:\python27\lib\site-packages\zmq\eventloop\zmqstream.py(440)_handle_events()
-> self._handle_recv()
  c:\python27\lib\site-packages\zmq\eventloop\zmqstream.py(472)_handle_recv()
-> self._run_callback(callback, msg)
  c:\python27\lib\site-packages\zmq\eventloop\zmqstream.py(414)_run_callback()
-> callback(*args, **kwargs)
  c:\python27\lib\site-packages\tornado\stack_context.py(275)null_wrapper()
-> return fn(*args, **kwargs)
  c:\python27\lib\site-packages\ipykernel\kernelbase.py(276)dispatcher()
-> return self.dispatch_shell(stream, msg)
  c:\python27\lib\site-packages\ipykernel\kernelbase.py(228)dispatch_shell()
-> handler(stream, idents, msg)
  c:\python27\lib\site-packages\ipykernel\kernelbase.py(390)execute_request()
-> user_expressions, allow_stdin)
  c:\python27\lib\site-packages\ipykernel\ipkernel.py(196)do_execute()
-> res = shell.run_cell(code, store_history=store_history, silent=silent)
  c:\python27\lib\site-packages\ipykernel\zmqshell.py(533)run_cell()
-> return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  c:\python27\lib\site-packages\ipython\core\interactiveshell.py(2717)run_cell()
-> interactivity=interactivity, compiler=compiler, result=result)
  c:\python27\lib\site-packages\ipython\core\interactiveshell.py(2821)run_ast_nodes()
-> if self.run_code(code, result):
  c:\python27\lib\site-packages\ipython\core\interactiveshell.py(2881)run_code()
-> exec(code_obj, self.user_global_ns, self.user_ns)
  <ipython-input-4-0922ee015237>(8)<module>()
-> print do_sth()
> c:\python27\lib\site-packages\ipykernel\iostream.py(342)write()
-> def write(self, string):
(Pdb) c #continue
I don't have time
  • 从脚本内部运行
%run -m pdb this_script.py
> f:\master_python\this_script.py(2)<module>()
-> import pdb
(Pdb) n
> f:\master_python\this_script.py(4)<module>()
-> def do_sth():
(Pdb) s
> f:\master_python\this_script.py(8)<module>()
-> print do_sth()
(Pdb) a
(Pdb) w
  c:\python27\lib\bdb.py(400)run()
-> exec cmd in globals, locals
  <string>(1)<module>()
> f:\master_python\this_script.py(8)<module>()
-> print do_sth()
(Pdb) c
> f:\master_python\this_script.py(6)do_sth()
-> return "I don't have time."

什么是调试

用于捕捉代码Bug。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值