Debugging Python Scripts
by Mandar Vaze on December 24, 2008
in Code, Python, Tutorials
Found a great way to debug python scripts interactively using pdb aka Python Debugger. It is similar to gdb used on *nix.
Essentially you import pdb in the beginning of the script, and wherever you need to start debugging, add following statement :
pdb.set_trace()
Now you execute the script from command line, and execution will stop where you have added set_trace() call. You are presented with pdb prompt. There after it is similar to gdb commands.
Here is the quick list of pdb commands
- n : Execute next line
- s : Step inside a module
- p : Print the value of a variable
- c : Continue
- l : List. Shows stack trace
- q : Quit (Most important command
)
For details, check this site
But what if I can’t debug interactively ?
Typically, you come across this problem when you are running the script on a server. Even if the script worked OK in test environment, in live environment something goes wrong, because the script is executed by server, and you can not control the arguments that are being passed to the script. In this scenario, you need to resort to age-old logging technique
Here are few line of code you need to add to your script. Modify as per your needs (especially paths)
Earlier in your script (as early as possible) add following lines :
from time import strftime
f = open('D:/temp/log.txt', 'r+')
f.write("-- Begin Log at %s --n" % strftime("%Y-%m-%d %H:%M:%S"))
At the end of your script (as late as possible) add following lines :
f.write("-- End Log at %s --n" % strftime("%Y-%m-%d %H:%M:%S"))
f.close()
Your basic logging infrastructure is ready. Now you can add f.write() lines through out your code, as and where needed.
Note : I’m part of pythondev group on twitter. Visit the website for details.

