pdb debugger

Date: January 8th 2016
Last updated: January 8th 2016

The pdb debugger is a built-in "interactive source code debugger". pdb lets you step through each line of code removing the need to comment and uncomment print statements. Also, pdb can be used as a script at the command line. For a more in depth description see https://docs.python.org/2/library/pdb.html.

Quick start commands:

command reference
n Single step progression in code.
p Print variable e.g. "p i" returns i
enter Carry over the last command.
u Move up the stack to a previous line.
d Move down the stack. Similar to n.
c Continue processing without debugging.
q Quit debugging and stop processing code.
h Print help

It is very tempting to add several print statements to debug the code below. Previously I would have put one above the if statement and another one after adding 1 to the count variable. But pdb can do debugging better.

To run pdb, uncomment the pdb.set_trace() command. In the output below, pdb showed that I have stuffed up the syntax on the line that adds 1 to the count variable. To check that this is the source of the error, I changed the assignment to count to count = count + 1. Sure enough the output returned 2! This line was then changed to count += 1.

Example:

#!bin/python2.7

import pdb

def count_vowels(x):
    """
    Returns the number of vowels in a string.
    """

    # uncomment to use pdb debugger
    # pdb.set_trace()

    # Set counter and vowel list
    count=0
    vowels=['a','e','i','o','u']

    # loop through each letter of the string
    for i in x:
        # match each letter to vowels
        if i in vowels:
            # add to counter
            count =+ 1  # <====================== ERROR

    # return number of vowels
    return count

# run the function
count_vowels('teest')

# Output (expect 2)
# 1

pdb at the Ipython prompt...


# pdb debugger in Ipython

### press n to step through code
-> count=0
(Pdb) n 

### press enter to carry over the last command entered into pdb 
-> vowels=['a','e','i','o','u']
(Pdb) <--enter-->

### print a variable with 'p' followed by the variable name
### the variable must be bound first before using 'p'
-> for i in x:
(Pdb) p i
*** NameError: NameError("name 'i' is not defined",)
(Pdb) n

### Now we have our first letter in the for loop
-> if i in vowels:
(Pdb) p i
't'
(Pdb) n

### Note the if statement above is false, we go back to the
### beggining of the for loop
-> for i in x:
(Pdb) <--enter-->

### Now we've found our first vowel
-> if i in vowels:
(Pdb) p count
0
(Pdb) p i
'e'
(Pdb) n

### count not added yet because we are currently on that line.
-> count =+ 1 
(Pdb) p count
0
(Pdb) n

### count added, now we can see that the for loop has worked on
### the first loop of finding a vowel.
-> for i in x:
(Pdb) p count
1
(Pdb) n

### I spelt 'test' wrong on purpose. We're now looping over 
### the second 'e' in my incorrectly spelt 'teest' string.
-> if i in vowels:
(Pdb) p i
'e'
(Pdb) n

### This should result in 2
-> count =+ 1
(Pdb) n

### Check the variable 'count' and notice that it's still one. 
### This is the root of our problem!!!
-> for i in x:
(Pdb) p count
1

# continue with the rest of the code without debugging...
(Pdb) c

# or quit processing your code...
(pdb) q

Useful resources:

results matching ""

    No results matching ""