The python debugger pdb
and its friends
All say: "Hello David!"
How do we tackle bugs?
How do we tackle bugs?
-
Intuitive solution: Misuse print() statements for debugging. Better in the long term: Add proper logging to your application!
- Ask questions: "What's hiding? Where does the bug occur? Is the bug new or old? What has changed? Did i make a bad assumption?"
- Ask yourself: Do i really understand the code where the error occurs?
- Do write unit tests for every non-trivial bug you encounter
- Read your version control history and do diffs
- Something to add?
What can we do with pdb?
- Interactive debugging
- Step through a program at runtime
- Stop code execution
- Break on your (conditional) breakpoints
- Modify and evaluate parts again and again
- Change stuff in memory
- Waste a lot of time if we don't have a
clue about what you're looking for! - A debugger is your last resort
... or at least never your first
What can we do with pdb?
PDB and friends
Shell: $> python -m pdb <yourcode.py> Inline: import pdb; pdb.set_trace()
at your service since Python 1.4 (1996)
- Get built-in help (help and ?)
- Show current context (list or list 3, 9 or longlist)
- Run script until next breakpoint (continue)
- Repeat last command (press return)
- Write some python code: for i in range 10: print(i)
- Print a value (print i or pp for pretty printing)
- Go to next line in current function (next)
- Step into a function (step)
- Print function parameters (args)
- Step outside of a loop (until)
- Skip to end of function (return)
-
Set break point (break) and remove them (clear)
- Set a conditional breakpoint ( `b <line>, a == 2017` )
- Navigating the execution stack (where, up, down)
- Start an interactive interpreter (interact, python3 only)
- Quit (quit)
More
-
.pdbrc (yes, it's configurable)
- Extensive writeup by Doug Hellmann
- pdbpp - drop in replacement for pdb (cake icing)
- ipdb - same but with fancy output and completion
- pudb - A full-screen, console-based Python debugger.
- pyringe - Debugger capable of attaching to and injecting code into Python processes.
- wdb - An improbable web debugger through WebSockets.
- winpdb - A Python Debugger with GUI, capable of remote debugging based on rpdb2.
-
PyCharm - unified debugging with comfortable GUI
- built-in watcher for variable change like pdbpp
pdb's friends
Back to David's tweet
It would seem that David and other renowned people from the community do not use/like/need debuggers: Your mileage may vary.
The python debugger pdb and its friends
By Stefan Antoni
The python debugger pdb and its friends
Beyond print()ing and log()ging
- 1,818