问题
When debugging my Python code, I run a script through ipdb from the commandline, and set a number of breakpoints. Then I make some changes in one or more modules, and rerun. However, if I simply use run modules do not get reloaded. To make sure they do, I can exist and restart Python completely, but then I need to reset all breakpoints, which is tedious if I have many and if done over and over again.
Is there a way to save breakpoint to a file in (i)pdb, so that after small changes that do not change line numbers, I can dump my breakpoints, restart Python + pdb, and reload my breakpoints? The equivalent to Matlabs X = dbstatus
, saving/loading X
, and setting dbstop(X)
.
回答1:
You can save the breakpoints to .pdbrc
file in a working path or globally to your home dir. File should have something like this:
# breakpoint 1
break /path/to/file:lineno
# breakpoint 2
break /path/to/file:lineno
You can define breakpoints various ways, just like in the interactive mode. So just break 4
or break method
will work too.
This file works for both, pdb and ipdb, since later has everything pdb has and more.
Bonus:
You could use alias
to more easily save breakpoints.
For example:
# append breakpoint to .pdbrc in current working directory
# usage: bs lineno
alias bs with open(".pdbrc", "a") as pdbrc: pdbrc.write("break " + __file__ + ":%1\n")
Put above to your global .pdbrc and use it like this:
> bs 15
This will append a breakpoint statement to a local .pdbrc file for a line 15 of current file.
It is not perfect solution, but close enough for me. Tune the command to your needs.
Read more about aliases here.
来源:https://stackoverflow.com/questions/28841520/save-breakpoints-to-file