This document discusses Python debuggers and how they can be used to step through code, inspect values, and help debug programs. It notes print statements are not sufficient for debugging and debuggers allow pausing execution and stepping line by line. Common debugger commands are described, and it recommends upgrading to ipdb or wdb which have additional features like colors and better interfaces. The document also mentions how to create a custom debugger by extending the bdb module in Python.
2. About these slides
These slides roughly correspond to a talk I
gave at a DC Python meetup on 2014-04-01.
During the actual talks I live-coded rather than
referring to slides, but wanted to make a
reference document for those that couldn’t
make it.
3. What’s a debugger?
Like a DVR for your program:
● pause at any time
● step through line by line
● inspect values
● execute code
4. Why do you need a debugger?
Debuggers are a powerful tool to help you
understand what your program is doing. They
are invaluable in tracking bugs, understanding
foreign code, and much more.
5. Print statements are not good enough
● You end up re-running your code countless
times inserting statements on random lines
● The output is unstructured and barely
intelligible
● Your codebase becomes littered with
accidentally committed debug statements
6. Debugging is easy!
Insert a breakpoint:
pdb.set_trace()
Start debugger after an exception:
pdb.pm() #post-mortem
7. PDB Commands
h(elp) - list commands or get help on a command
b(reak) - insert a breakpoint
c(ontinue) - keep running until you hit a breakpoint
s(tep) - continue until the next line executed (step into a function)
n(ext) - continue until the next line in the current function
r(eturn) - continue until the current function returns
For a full list of commands (with better explanaitons), use the help command or
consult the documentation
8. Upgrade to ipdb!
install: sudo pip install ipdb
usage: s/pdb/ipdb/
… and that’s it! It works very similar, but with a few
improvements:
● better context for traces
● colors
● more commands
● tab completion
9. Upgrade to wdb!
install: sudo pip install wdb
usage: s/pdb/wdb/
have to run the server in the background: wdb.server.py &
● Launches a browser window
○ great for situations where you don’t have access to the
terminal (like web servers) but can be used for any
program
● awesome interface (easily jump around the call stack)
● better tab completion
● can access from other machines
10. Making your own debugger
Extend bdb.Bdb and override the user_* functions
You can do whatever you want on function call/return,
exception, and line executed:
* write a profiler
* visualize your code execution
* make your ide smart about code usage: what functions
actually call this one when we run our code? How often is
this line executed?
11. My pet project
I’ve been writing a program that logs every line, function call, return, and
exception to a file with timestamps and other important metadata.
That part is working as a proof of concept (not terribly efficient)
I hope to write multiple programs that can read that output and help developers
better understand what their code is doing (what calls what, how long do things
take, visualize code coverage, step through programs after they’ve finished
running.
I hope to post an initial version of this to github very soon. If you’re interested
and want to talk, get updates, or help, please ping me on github/mattboehm or
boehm.matthew@gmail.com
12. Thanks! About me:
https://github.com/mattboehm
@mattboehm
Founder of the DC/NoVA Vim Meetup: http://bit.ly/dcvim
(I hope you can make it out to an event-- you’ll learn a lot no matter what your experience level)
Python/JS developer at Novetta Solutions
(I love my job-- talk to me if you’re looking for something new)
Talk to me any time about:
Vim, Python, AngularJS, Git, Linux, dvorak, mechanical/ergonomic keyboards, standing desks, meetups,
podcasts, board games, or anything else that strikes your fancy