admin管理员组

文章数量:1410705

I often have to debug the python code by pdb debugger. It happens that it fails due to program errors that should be corrected. The problem is that I have to update the python code and restart the debugger to reach the point where the problem ealier ocurred. It takes time for not trivial programs. Is there a way to keep that python debugger state of execution and have the option to change the code being debugged? Alternatively, is there a way to make some kind of results snapshot of python program time consuming initialication and then try debuggind the code? In the later case I want to restart the program from the snapshot and continue with the updated part of python program.

For debugging I use command line pdb initiated by the breakpoint() or pdb.set_trace() commands.

I often have to debug the python code by pdb debugger. It happens that it fails due to program errors that should be corrected. The problem is that I have to update the python code and restart the debugger to reach the point where the problem ealier ocurred. It takes time for not trivial programs. Is there a way to keep that python debugger state of execution and have the option to change the code being debugged? Alternatively, is there a way to make some kind of results snapshot of python program time consuming initialication and then try debuggind the code? In the later case I want to restart the program from the snapshot and continue with the updated part of python program.

For debugging I use command line pdb initiated by the breakpoint() or pdb.set_trace() commands.

Share edited Mar 10 at 8:38 Anatoly Osipov asked Mar 10 at 8:19 Anatoly OsipovAnatoly Osipov 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

So, pdb doesnt support directly 'saving' your state of program, however, the other ways to debug.

  • For example code module, it has functionallity to update your code during debugging, code.interact(local=globals()), this allow you to execute your program with any changes during debugging. docs

  • Also you can use IPython, but its more advanced. Anyways, you can start using it with replacing your pdb.set_trace with:

from IPython import embed
embed()

Other options is pytest, but its useful only if you are working in test-driven env. There you can debug your tests without restarting the whole application, pytest --pdb helps you.

本文标签: updating python code in the middle of debuggingStack Overflow