May 222010
How to debug in Python?
Answer:
Assume you have a simple python script like:
a = 1
b = 2
c = a + b
print c
To enable debugging, add the following line at the top of the program
import pdb
and in the line you want to break, add
pdb.set_trace()
So the whole program become:
import pdb
a = 1
b = 2
pdb.set_trace()
c = a + b
print c
When you execute the script by python test.py, you will in the debug mode.
Some useful commands:
1. Print variables: p a
2. Step over: n
3. Continue: c
4. Quit: q