Skip to content
February 13, 2012 / Kamil Páral

The beauty of Pdb

I never used Pdb much. I always thought that I had to run my whole script through the debugger, set some breakpoints through obscure commands and whatnot. But it is not the case. I can do simply this:

#!/usr/bin/env python
do_some_stuff()
import pdb
pdb.set_trace()
do_some_other_stuff()

Now you run your script as usual, and it stops at the pdb.set_trace() line and opens up a Pdb prompt. Then you can go line by line, explore and set variable contents and find out the source of your problem. Great! This approach is convenient and easy to use.

You need to know just a several basic commands to be able to do quite a lot in the Pdb shell. Read this 10-minute introduction, it’s worth it: Debugging in Python

Enjoy.

Flattr this

About these ads

One Comment

Leave a Comment
  1. Pádraig Brady / Sep 23 2012 19:04

    This technique is really useful and often overlooked. Here are notes on using it in various languages http://www.pixelbeat.org/programming/trigger_debug.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: