Easy way to put PDB breakpoints in Python code?

Just a matter of convenience. I am a bit spoiled by debuggers in the IDE, such as Visual Studio and Xcode. I'm a little uncomfortable typing import pdb; pdb.set_trace() import pdb; pdb.set_trace() to set a breakpoint (I would prefer not to import pdb at the top of the file, as I could forget and leave it).

Is there an easier way to set a breakpoint in Python code, as simple and unobtrusive as what you see in the IDE?

+93
python pdb
Aug 08 2018-11-11T00:
source share
11 answers

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

python -m pdb your_script.py

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

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

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

+106
Aug 08 2018-11-11T00:
source share

You can use:

 from pdb import set_trace as bp code code bp() code code 
+52
Feb 24 '15 at 13:56
source share

In vim, I have a macro configured 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 that I can just press \ b (when not in insert mode), and it adds a breakpoint after the current line, or \ B (note the uppercase), and it puts a unit in front of the current line.

which seems to work well. Most other "simple" programmer editors (emacs, sublimetext, etc.) should have similar simple 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 includes it only for Python source files. You can very easily add similar strings for JavaScript or any other languages ​​that you use.

2019 update (Python 3. 7+)

Python 3. 7+ now has a breakpoint() built-in that can replace the previous import pdb; pdb.set_trace() import pdb; pdb.set_trace() import pdb; pdb.set_trace() import pdb; pdb.set_trace() in vim. It still works the same.

+33
Oct 28 '13 at 11:51 on
source share

If you don’t want to manually set breakpoints each time the program starts (in Python 3. 2+), for example, say you want to directly create a breakpoint on line 3 and stop execution there:

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

The following information may help:

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

Changed in version 3.2: .pdbrc can now contain commands that continue debugging, for example, continue or further. Previously, these commands had no effect.

New in version 3.2: pdb.py now accepts the -c option, which executes the commands as if they were specified in the .pdbrc file, see Debugger Commands.

+22
Nov 19 '15 at 15:42
source share

I have not tried it yet, but they just implemented the new built-in breakpoint () function in Python 3.7, which means you can insert a breakpoint with one statement now:

 breakpoint() 
+15
Mar 23 '18 at 0:26
source share

Here is how you would use pdb on the command line without implementing anything in your source code (documentation and other online resources do not explain this very well to a programmer who in the past used only visual debuggers):

Run pdb by typing the following at a command prompt:

 python -m pdb 'python_script' 

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

 (Pdb) 

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

You can go to the next line in your code with the "n" command (next):

 (Pdb) n 

Doing the following will display the line number and specific code in the source:

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

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

 (Pdb) b 50 

Here, the debugger is set to break on line 50. If there are no other breakpoints, the breakpoint on line 50 will be the first, and a breakpoint identifier of 1 can refer to it. If you add more breakpoints, they will get the identifiers sequentially (i.e. 2, 3, etc.)

Once the breakpoint is set, you continue to run the program until pdb reaches the breakpoint as follows:

 (Pdb) c 

When you reach the breakpoint, you can go to the next line with the n command, as described above. If you want to check the values ​​of variables, you must execute the parameter command as follows:

 (Pdb) p variable_name 

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

 (Pdb) clear 1 

Finally, when you are done with the debugger, you can exit the execution in the same way as from the python command line interpreter.

 (Pdb) exit() 

I hope this helps anyone get started with pdb. Here is a list of commands you can use with the debugger: pdb, so questions and answers

+10
Aug 16 '16 at 20:06
source share

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

http://winpdb.org/

+4
Aug 08 2018-11-11T00:
source share

Python 3.7 has a new built-in way to set breakpoints. vocation

 breakpoint() 

More details here https://stackoverflow.com/a/312960/

+3
Nov 12 '18 at 13:27
source share

You can use:

  • ide wing
  • Eclipse with pydev plugin
  • pycharms

All of the above supports python debugging from within the development environment.

+2
Aug 08 '11 at 10:30
source share

In Atom, if Python plugins are installed, you can just type " pdb " and press "Enter", and the snippet will enter the import and trace for you.

I'm used to it now that sometimes I just print it, even if I edit it in vim and expect a dropdown to appear.

0
May 7 '17 at 10:56 PM
source share

The easiest way to run a debugger on your script is simply

 pdb your_script.py 

Running pdb on a Linux command line gives

 usage: pdb.py scriptfile [arg] ... 
-one
Feb 07 '14 at 6:43
source share



All Articles