将PDB断点放入Python代码的更简单方法?

问题:将PDB断点放入Python代码的更简单方法?

只是一个方便的问题。我对诸如Visual Studio和XCode之类的IDE中的调试器非常满意。我发现必须键入import pdb; pdb.set_trace()设置断点有点笨拙(我宁愿不要将pdb导入文件的顶部,因为我可能会忘记并将其保留在里面)。

是否有更简单的方法在Python代码中设置断点,就像在IDE中看到的那样简单明了?

Just a convenience question. I’ve been a bit spoiled with debuggers in IDEs like Visual Studio and XCode. I find it a bit clumsy to have to type import pdb; pdb.set_trace() to set a breakpoint (I’d rather not import pdb at the top of the file as I might forget and leave it in).

Is there a simpler way of setting a breakpoint in Python code, as straightforward and unobtrusive as what you see in an IDE?


回答 0

您可以pdb通过以下方式从命令行运行程序

python -m pdb your_script.py

它将在第一行中断,然后您可以使用以下break命令在代码中的任意位置添加断点,其语法为:

b(reak)[[文件名:] lineno | 功能[,条件]]

它足够灵活,可以让您在任何地方添加断点。

You can run your program into pdb from the command line by running

python -m pdb your_script.py

It will break on the 1st line, then you’ll be able to add a breakpoint wherever you want in your code using the break command, its syntax is:

b(reak) [[filename:]lineno | function[, condition]]

It is flexible enough to give you the ability to add a breakpoint anywhere.


回答 1

您可以使用:

from pdb import set_trace as bp

code
code
bp()
code
code

You can use:

from pdb import set_trace as bp

code
code
bp()
code
code

回答 2

在vim中,我为此设置了一个宏(在我的.vimrc文件中):

map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>

所以我可以按\ b(不在插入模式下),然后在当前行之后添加一个断点,或者在\ B(注意大写)处添加一个断点,并将它放在当前行之前。

这似乎工作正常。其他大多数“简单”的程序员编辑器(emacs,sublimetext等)也应具有类似的简便方法。

编辑: 我实际上有:

au FileType python map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
au FileType python map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>

仅针对python源文件将其打开。您可以非常轻松地为javascript或您使用的任何其他语言添加类似的行。

2019更新(Python 3.7+)

Python 3.7+现在具有内置功能breakpoint(),可以代替import pdb; pdb.set_trace()vim中的先前功能。它仍然起作用。

In vim, I have a macro set up for this (in my .vimrc file):

map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>

so I can just press \b (when not in Insert Mode) and it adds in a breakpoint after the current line, or \B (note the capital) and it puts one before the current line.

which seems to work alright. Most other ‘simple’ programmers editors (emacs, sublimetext, etc) should have similar easy ways to do this.

Edit: I actually have:

au FileType python map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
au FileType python map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>

which turns it on only for python source files. You could very easily add similar lines for javascript or whatever other languages you use.

2019 Update (Python 3.7+)

Python 3.7+ now has the builtin breakpoint() which can replace the previous import pdb; pdb.set_trace() in vim. It still works the same.


回答 3

如果您不想每次运行程序时都手动设置断点(在Python 3.2+中),例如说您想直接在第3行创建一个断点并在此处停止执行:

python -m pdb -c "b 3" -c c your_script.py

以下信息可能会有所帮助:

如果文件.pdbrc在用户的主目录或当前目录中存在,则将其读入并执行,就像在调试器提示符下键入该文件一样。这对于别名特别有用。如果两个文件都存在,则首先读取主目录中的文件,并且本地文件可以覆盖其中定义的别名。

在版本3.2中进行了更改:.pdbrc现在可以包含继续调试的命令,例如continue或next。以前,这些命令无效。

3.2版中的新增功能:pdb.py现在接受-c选项,该选项执行命令的方式就像在.pdbrc文件中给出的一样,请参阅调试器命令。

If you don’t want to manually set breakpoints every time running the program (in Python 3.2+), e.g. say you want to directly create a breakpoint at line 3 and stop the execution there:

python -m pdb -c "b 3" -c c your_script.py

The following information may help:

If a file .pdbrc exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file.

Changed in version 3.2: .pdbrc can now contain commands that continue debugging, such as continue or next. Previously, these commands had no effect.

New in version 3.2: pdb.py now accepts a -c option that executes commands as if given in a .pdbrc file, see Debugger Commands.


回答 4

我还没有尝试过,但是他们只是在Python 3.7中实现了一个称为breakpoint()的新内置函数,这意味着您现在可以使用一条语句插入一个断点:

breakpoint()

I haven’t tried it yet but they just implemented a new built-in called breakpoint() in Python 3.7 which means you can insert a breakpoint with one statement now:

breakpoint()

回答 5

这是您在命令行中使用pdb而不在源代码中执行任何操作的方式(文档和其他在线资源不能很好地向过去仅使用可视调试器的程序员解释这一点):

通过在shell提示符下键入以下内容来启动pdb:

python -m pdb 'python_script'

此命令将初始化pdb,并且pdb调试器将在python_script的第一行中断,并等待您的输入:

(Pdb)

这是与调试器进行通信的接口。现在,您可以在此处指定命令。与在视觉调试器中使用按钮或键盘快捷键相反,此处将使用命令来获得相同的结果。

您可以通过命令“ n”转到下一行代码(下一个):

(Pdb) n

执行下一步将显示行号以及源代码中的特定代码:

> python_script(line number)method name
-> current line in the source code

您可以通过在源代码中指定行号来设置断点。

(Pdb) b 50

在这里,调试器设置为在第50行中断。如果没有其他任何断点,则第50行的断点将是第一个,在这种情况下,断点ID可以引用该断点。如果您添加更多的断点,它们将依次获得标识符(即2、3等)

设置断点后,您将继续执行程序,直到pdb到达断点,如下所示:

(Pdb) c

一旦到达断点,就可以使用前面所述的n命令转到下一行。如果要检查变量的值,则可以按如下所示执行parameter命令:

(Pdb) p variable_name

如果您不再需要断点,则可以通过使用clear命令传递断点的ID来清除它:

(Pdb) clear 1

最后,完成调试器后,您可以退出执行,就像退出python命令行解释器一样。

(Pdb) exit()

我希望这可以帮助任何人开始使用pdb。这是可以与调试器一起使用的命令列表:pdb so问题与解答

This is how you would use pdb in the command line without implementing anything in your source code (the documentation and other online resources don’t do a good job explaining this to a programmer who has only used visual debuggers in the past):

Start pdb by typing the following in a shell prompt:

python -m pdb 'python_script'

This command initializes pdb and the pdb debugger will break at the first line of your python_script and wait for an input from you:

(Pdb)

This is the interface for communicating with the debugger. Now, you can specify your commands here. Opposed to using buttons or keyboard shortcuts in visual debuggers, here you will use commands to derive the same results.

You can go to the next line in your code by command “n” (next):

(Pdb) n

Performing a next would display the line number, and the specific code in the source:

> python_script(line number)method name
-> current line in the source code

You can set a breakpoint by specifying a line number in your source code.

(Pdb) b 50

Here, the debugger is set to break at line 50. If there aren’t any other breakpoints, the breakpoint at line 50 will be the first and it could be referenced by the breakpoint id which is 1 in this case. If you add more break points they will get identifiers sequentially (i.e., 2, 3 etc.)

Once a breakpoint is set, you would continue executing your program until pdb gets to the breakpoint as follows:

(Pdb) c

Once you get to a breakpoint you could go to the next line, with the n command as described before. If you want to examine the values of variables, you would execute the parameter command as follows:

(Pdb) p variable_name

If you no longer need a breakpoint, you can clear it by passing in the id of the breakpoint with the clear command:

(Pdb) clear 1

Finally, when you are done with the debugger you can exit the execution as you would exit the python command line interpreter.

(Pdb) exit()

I hope this will help anybody get started with pdb. Here is a list of commands you can use with the debugger: pdb so question and answers


回答 6

Python 3.7具有设置断点的新内置方式。呼唤

breakpoint()

更多内容https://stackoverflow.com/a/53263117/6488361

Python 3.7 has a new builtin way of setting breakpoints. Calling

breakpoint()

More here https://stackoverflow.com/a/53263117/6488361


回答 7

您可以使用支持python调试的IDE,或者可以查看出色的Winpdb工具。它可以在任何平台上运行,并为python脚本提供图形化调试功能。

http://winpdb.org/

You could use an IDE which supports python debugging, or you can check out the excellent Winpdb tool. Which works on any platform and provides graphical debugging facilities to your python script.

http://winpdb.org/


回答 8

您可以使用:

  • 翼翼
  • 用pydev插件蚀
  • pycharms

以上所有功能都支持从IDE内部进行python调试。

You can use:

  • wing ide
  • eclipse with the pydev plugin
  • pycharms

All of the above support python debugging from inside an IDE.


回答 9

在Atom(如果安装了Python插件)中,您只需输入“pdb ‘并按Enter,该代码段将为您键入import和trace back。

我已经习惯了这一点,即使我在vim中编辑它并等待下拉菜单出现,有时也只是键入它。

In Atom if Python plugins installed, you can just type in ‘pdb‘ and hit enter and the snippet will type import and trace back for you.

I’ve used to this now that sometimes I just type it in even if I’m editing it in vim and waiting for the dropdown to appear.


回答 10

一种被低估的方法是直接在pdb中设置断点:

pdb> b torch/__init__:10

将在 site-packages\torch\__init__.py:10

然后pdb> c将在此断点处停止。

以下内容也有效:

pdb> b d:\anaconda\lib\site-packages\torch\__init__.py:10
pdb> b torch\__init__.py:10
pdb> b d:\\anaconda\\lib\\site-packages\\torch\\__init__.py:10
pdb> b d:/anaconda/lib/site-packages/torch/__init__.py:10

有关详细信息,请参见文档

A underrated method is to set breakpoint in pdb directly:

pdb> b torch/__init__:10

will set a breakpoint on site-packages\torch\__init__.py:10

Then pdb> c will stop on this breakpoint.

Following are valid too:

pdb> b d:\anaconda\lib\site-packages\torch\__init__.py:10
pdb> b torch\__init__.py:10
pdb> b d:\\anaconda\\lib\\site-packages\\torch\\__init__.py:10
pdb> b d:/anaconda/lib/site-packages/torch/__init__.py:10

See doc for detail.


回答 11

您可以将Vim与Python-Mode插件一起使用,或者将Emacs与Elpy插件一起使用。

这些插件可通过简单的击键(\ b在Vim和C-c C-u bEmacs中)为您提供断点,以及重量级IDE的许多其他功能(代码折叠,重构,整理等),所有这些都在基于终端的轻量级文本编辑器中完成。

You could use Vim with the Python-Mode plugin or Emacs with the Elpy plugin.

These plugins give you breakpoints with easy keystrokes (\ b in Vim and C-c C-u b in Emacs) plus many other features from heavy-weight IDEs (code folding, refactoring, linting, etc.) – all within a light-weight terminal-based text editor.


回答 12

在脚本上运行调试器的最简单方法是

pdb your_script.py

在Linux命令行上运行pdb

usage: pdb.py scriptfile [arg] ...

The simplest way to run the debugger on your script is just

pdb your_script.py

Running pdb on a Linux command-line gives

usage: pdb.py scriptfile [arg] ...