Python Debugging Snippets
PDB is Python Debugger. It is in the standard library though there are other debuggers and GUI ones.
# Debug a script with Python Debugger (PDB), part of standard librarypython -m pdb pyfile.py# See https://docs.python.org/3/library/pdb.html# Commands are similar to GDB
# Debug interactively with PDBimport pdbimport buggyscriptbuggyscript.crash()# Post mortem command to PDB at crashpdb.pm()
# Start debugging in program with consoleimport pdbpdb.set_trace()# run program with PDB console
# Set a breakpoint in a programbreakpoint()
PDB Console
Section titled “PDB Console”Also accepts valid python
# Helphelp
# list, see current code line - same as GDBl
# next, go through next line of code - same as GDBn
# continue, until next breakpoint - same as GDBc
# step into a statement, function - same as GDBs
# returnr
# print variables - same as GDBp variable_name
# breakpoint set on line of current fileb 20break 4
# Breakpoint in temporary place, breakpoint will only occur once - same as GDBtbreak 6
# unt(il) line numberuntil 6# Continue execution until the line with a number greater# than the current one is reached or until the current frame returns
See Also
Section titled “See Also”- GDB and LLDB Snippets - GDB and LLDB Debugger Snippets for similar commands