如何使用调试
- 从命令行运行
# -*- 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。