Difference between revisions of "Python pdb"

From John Freier
Jump to: navigation, search
 
Line 4: Line 4:
  
 
   import pdb
 
   import pdb
 +
 +
To add a breakpoint in the python use
 
   pdb.set_trace()
 
   pdb.set_trace()
 +
  or
 +
  breakpoint() # 3.7 for higher
  
 
And then just add the following to your command.
 
And then just add the following to your command.
Line 33: Line 37:
 
   !<expr> execute the expression <expr> NOTE: this acts just like a python interpreter
 
   !<expr> execute the expression <expr> NOTE: this acts just like a python interpreter
 
   q(uit)  exit the debugger
 
   q(uit)  exit the debugger
 
 
To add a breakpoint in the python.
 
  breakpoint()
 

Latest revision as of 08:22, 29 May 2024

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