Python pdb

From John Freier
Jump to: navigation, search

To debug python applications use the library pdb.

Add the following to the top of your file.

 import pdb

To add a breakpoint in the python use

 pdb.set_trace()
 or
 breakpoint() # 3.7 for higher

And then just add the following to your command.

 --pdb

Example

 pytest --pdb test/test.py

Once it starts up you will be brought to a promote. From the there you can use any of the following.

Navigating Code (within the Pdb interpreter)

 l(ist)   list 11 lines surrounding the current line
 l .      list the current location
 w(here)  display the file and line number of the current line
 n(ext)   execute the current line
 s(tep)   step into functions called at the current line
 r(eturn) execute until the current function’s return is encountered

Controlling Execution

 b[#]       create a breakpoint at line [#]
 b          list breakpoints and their indices
 c(ontinue) execute until a breakpoint is encountered
 clear[#]   clear breakpoint of index [#]

Changing Variables / Interacting with Code

 p<name> print value of the variable <name>
 !<expr> execute the expression <expr> NOTE: this acts just like a python interpreter
 q(uit)  exit the debugger