Wednesday, May 5, 2010

Python debugging quickie

I've gotten into the habit of using pdb to interactively debug python code. I'll plug import pdb; pdb.set_trace() into the middle of an uncooperative function in order to examine machine state as the code runs.  (Note that I don't need to full capabilities of a debugger here; I'm not stepping through the code line-by-line.)

iPython is a much better choice, as this posting lays out:
import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()

That's all you need to drop out of your function and into an iPython shell. When you're done twisting knobs, ctrl-D will exit iPython and return to your function.

2 comments:

sebastian López said...

i want to know how to embedding IPhyton into wx.Panel

my IPython version is 0.12 and the wx version is 2.9.3.1

thanks

kieran said...

Oh my, that sounds tough... I'm not sure I know where to begin, particularly as I haven't used wx at all.

That being said, if you run your python script from a terminal (or, under windows, from a command prompt), calling IPython.embed() causes an IPython prompt to appear in the terminal from which you ran your script, as you would expect. (Note that this syntax has changed a little bit.) I tested this using the "Hello World" code from here:

http://wiki.wxpython.org/Getting%20Started

I modified it slightly to replace the "About" menu option with a "Debug" option which invokes iPython.

But I don't think that's really what you're asking: I suspect you want iPython to run in the application window itself, and not just in the command prompt. I really don't know how to go about that, though something like wxPython's shell module might be a good start:

http://wxpython.org/docs/api/wx.py.shell.Shell-class.html