7

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).

4

1 回答 1

6

您可以将断点保存到.pdbrc工作路径中的文件或全局保存到您的主目录。文件应该是这样的:

# breakpoint 1
break /path/to/file:lineno

# breakpoint 2
break /path/to/file:lineno

您可以通过多种方式定义断点,就像在交互模式中一样。所以只是break 4break method也会起作用。

该文件适用于 pdb 和 ipdb,因为以后有 pdb 的所有内容等等。

奖金

您可以使用alias更轻松地保存断点。例如:

# 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")

将上面放到您的全局 .pdbrc 中并像这样使用它:

> bs 15

这会将断点语句附加到当前文件的第 15 行的本地 .pdbrc 文件中。

这不是完美的解决方案,但对我来说足够接近。根据您的需要调整命令。

在此处阅读有关别名的更多信息。

于 2016-11-21T23:29:18.763 回答