Monday, July 21, 2008

Python debugging

So I just discovered the awesomeness that is ipython -- specifically, the awesomeness that is the ipython embedded shell.

Ever wished you could just stop at a given point in your program and start poking around using the handy dandy python shell? Here's how.

# We clear the args because arguments to your program will confuse IPython
import sys
sys.argv = []
# And now we embed a shell...
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell()

This even works from in the midst of a GUI (I use it in the midst of my gtk program).

For a lazy programmer like me, this is a godsend. Now rather than reading back through my code to find out what I need to do, I can plop the below code wherever I'm in the midst of working, then find myself landed in a shell where I can inspect my objects, variables, etc., figure out just what I need to add to my code, and give it a test run.

No comments: