All Unkept
Posted in: Python  —  6 May 2010

Exploratory programming with IPython

So, IPython rocks. But what if you want to use it deep inside a program to do a bit of exploratory programming with access to the full program state etc.? Just stick these two lines of code into the place you want to start off from:

from IPython.Shell import IPShellEmbed
IPShellEmbed([])()

More info on IPShellEmbed here.

I just discovered this, and it is unbelievably useful in some situations!

Comments §

§ On 7 May 2010, Jeff wrote: 870
I found this gem few months back and I havent looked back ever since. My debug print statements have dropped a lot ever since.

Although I should mention I use a different flavor of the same idea. I first drop to pdb and then invoke ipython from there. That way when I quit out of ipython, i can go back and forth and maybe go into ipython again.



the way I do is.

import pdb;pdb.set_trace() > this goes inside the program

and inside pdb prompt
(pdb) invoke ipython

§ On 7 May 2010, Art wrote: 871
@Jeff
There is no such command in pdb as 'invoke'. Can you correct this? I find this technique very interesting.

§ On 7 May 2010, Kevin wrote: 872
Similar to pdb there is the ipython version ipdb.

import ipdb; ipdb.set_trace()


§ On 7 May 2010, Jeff wrote: 873
@ Art

Sorry if confused you folks. When I said 'invoke ipython' I meant invoke ipython in the way mentioned in the article.

§ On 9 May 2010, jelle wrote: 874
ipdb is a wonderful contribution. would be cool to see it merged into IPython itself...

§ On 10 May 2010, whoa wrote: 876
Whoa! nice one thanks.

§ On 14 May 2010, Dan wrote: 877
Don't forget winpdb as an alternative to pdb or ipdb. It can attach to an already running program when you use set_trace().

§ On 20 May 2010, Funkmyster wrote: 878
Sweet! Something so simple yet so awesomely useful :)
I'm not sure if I'll ever use print to debug again

§ On 10 July 2011, Canada pharmacies wrote: 1230
This is a good informational blog.That blog is very very importat for all human to know for there it sector and this blog authority.Thank you so much for the blog.

Add comment

Format:

  • Javascript has to be on to get past my spam protection, and cookies, and there is a delay, sorry for any inconvenience!
  • I reserve the right to moderate comments.