Debugging Python

Python Script Debugging Using pdb | Example Debugging Session

Python Overview | Debugging Python | Python Site Packages

Python Script Debugging Using pdb

To use pdb as a stand-alone debugger, type:

python -m pdb myscript.py

Consult the Debugger Commands section of the Python Library Reference for the list of available debugger commands.

Example Debugging Session

import os, sys
import string

def plusAndSquare(a, b):

res = 0
res = (a + b)
res = res * res

print '('+`a`+'+'+`b`+')^2 = ' + `res`

def main():

print "Enter the first number:"
first = sys.stdin.readline()
print "Enter the second number:"
second = sys.stdin.readline()
plusAndSquare(string.atoi(first),
string.atoi(second))

main()

 

example.py

This script asks for two numbers from the user then adds them and squares the result.

%python -m pdb example.py
> /g/g2/dahn/DEBUG/PY/<string>(0)?()
(Pdb) b main <- Set a breakpoint at main function
Breakpoint 1 at /g/g2/dahn/DEBUG/PY/example.py:17
(Pdb) c <- continue
> /g/g2/dahn/DEBUG/PY/<string>(1)?()
(Pdb) c <- continue until we hit a breakpoint
> /g/g2/dahn/DEBUG/PY/example.py(17)main()
-> print "Enter the first number:"
(Pdb) l <- list the source code showing where we are
12 print '('+`a`+'+'+`b`+')^2 = ' + `res`
13
14
15 def main():
16
17 B-> print "Enter the first number:"
18 first = sys.stdin.readline()
19 print "Enter the second number:"
20 second = sys.stdin.readline()
21 plusAndSquare(string.atoi(first), string.atoi(second))
22
(Pdb) b plusAndSquare <- Set a breakpoint at another function
Breakpoint 2 at /g/g2/dahn/DEBUG/PY/example.py:7
(Pdb) c <- continue until we hit a breakpoint
Enter the first number:
4
Enter the second number:
7
> /g/g2/dahn/DEBUG/PY/example.py(7)plusAndSquare()
-> res = 0
(Pdb) l <- list the source code showing where we are
2 import os, sys
3 import string
4
5 def plusAndSquare(a, b):
6
7 B-> res = 0
8
9 res = (a + b)
10 res = res * res
11
12 print '('+`a`+'+'+`b`+')^2 = ' + `res`
(Pdb) n <- next
> /g/g2/dahn/DEBUG/PY/example.py(9)plusAndSquare()
-> res = (a + b)
(Pdb) n <- next
> /g/g2/dahn/DEBUG/PY/example.py(10)plusAndSquare()
-> res = res * res
(Pdb) n <- next
> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()
-> print '('+`a`+'+'+`b`+')^2 = ' + `res`
(Pdb) l <- list the source code showing where we are
7 B res = 0
8
9 res = (a + b)
10 res = res * res
11
12 -> print '('+`a`+'+'+`b`+')^2 = ' + `res`
13
14
15 def main():
16
17 B print "Enter the first number:"
(Pdb) p res <- print what value a variable, res, has
121
(Pdb) w <- print the stack trace of the current frame
/g/g2/dahn/DEBUG/PY/<string>(1)?()
/g/g2/dahn/DEBUG/PY/example.py(23)?()
-> main()
/g/g2/dahn/DEBUG/PY/example.py(21)main()
-> plusAndSquare(string.atoi(first), string.atoi(second))
> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()
-> print '('+`a`+'+'+`b`+')^2 = ' + `res`
(Pdb) a <- print the argument list of the current frame
a = 4
b = 7
(Pdb) n <- next
(4+7)^2 = 121
--Return--
> /g/g2/dahn/DEBUG/PY/example.py(12)plusAndSquare()->None
-> print '('+`a`+'+'+`b`+')^2 = ' + `res`
(Pdb) n <- next
--Return--
> /g/g2/dahn/DEBUG/PY/example.py(21)main()->None
-> plusAndSquare(string.atoi(first), string.atoi(second))
(Pdb) n <- next
--Return--
> /g/g2/dahn/DEBUG/PY/example.py(23)?()->None
-> main()
(Pdb) n <- next
--Return--
> /g/g2/dahn/DEBUG/PY/<string>(1)?()->None
(Pdb) n <- next

Top