python pdb

abstract

pdb is Python's built-in debugger.

basics

commands

run code
c continue until breakpoint
h show help menu
interact start interpreter
n run until next line
q quit debugger
r run until function returns
run run script again
s step inside function
unt 13 run until line 13
!statement execute statement
show context
args arguments for current function
d 5 down 5 levels in stack trace
l list lines near current line
ll list lines in current frame
l 13 list lines near line 13
l 13 20 list lines from 13 through 20
p x print value of x
pp x pretty-print value of x
source x source code of x
w where are we in stack trace?
u 5 up 5 levels in stack trace
set breakpoints
b show breakpoints
b 13 set breakpoint at line 13
b func set breakpoint inside func()
cl clear all breakpoints
cl 3 clear 3rd breakpoint

dependencies

pdb is included with Python.

examples

Run pdb like a script:

python -m pdb

Run path/to/myscript.py and start pdb:

python -m pdb path/to/myscript.py

Start pdb from inside a Python interpreter:

python3
Python 3.7.0 (default, Aug 17 2018, 21:14:48)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> breakpoint()
--Return--
> <stdin>(1)<module>()->None

faq

Where are the official docs?
pdb module docs
Where can I find other tutorials?
DigitalOcean pdb tutorial
Djangostars pdb tutorial
Email GitHub LinkedIn
return
© Sam Kennerly 2023. Licensed under a CC BY 4.0 license.
Built by a Quarto
with icons by Simple Icons.
samkennerly@gmail.com