Published on

Debug in Python

Authors
  • avatar
    Name
    Loc Truong
    Twitter

Pdb

Start pdb inside a python script

import pdb;pdb.set_trace()
breakpoint()

Start pdb from the command line

python -m pdb <file.py>
# Stepping
n(ext)Step over
s(tep)Step into
r(eturn)Continue until the current function returns
c(ontinue)Continue until the next breakpoint is encountered
unt(il) line_numberContinue until a specific line is encountered. Or continue until a larger line number is reached if no line_number is set
u(p)Up one level in the stack trace
d(own)Down one level in the stack trace
h(elp) OR h(elp) commandShow help
q(uit)Quit debugger

Breakpoint

b(reak)Show all breakpoints
b(reak) line_numberSet a breakpoint at a specific line
b(reak) line_number, conditionSet a breakpoint at a specific line, if condition is met
b(reak) file:line_numberSet a breakpoint in a file at a specific line
b(reak) funcSet a breakpoint at the first line of a function
disable numberDisable breakpoint number
enable numberEnable breakpoint number
clear numberRemove breakpoint number

Printing

p(rint) exprpp exprPrint the value of expr
w(here)Print current position and stack trace
l(ist)l(ist) start, endPrint 11 lines of code around the current line
a(rgs)Print the arguments of the current function