Jason Meridth's Blog Learn, Converse, Share

How To Use the Python Debugger aka pdb

I have been using the Python Debugger aka pdb docs for 2.7/docs for 3.5 a lot recently and wanted to share a cheat sheet I found and also the common commands I use.

The cheatsheet:

pdb cheatsheet

The easiest thing to do is put the following in your code somewhere:

    import pdb; pdb.set_trace()

Then when you run it, it will stop there and you can step through the code a line at a time. The l(ist) command will show you 11 lines of code around the current line is the one I use the most. s(tep), n(ext), r(eturn) are some other I use regularly.

Once the debugger is up, you can check the stacktrace w(where), variable values a(rgs) or [variable_name] or many other things. Please check the documentation or the cheatsheet image above.

A much more in-depth usage of pdb can be found here

Cheatsheet image credit

Have feedback on this post? Let @jmeridth know on Twitter.