Saturday, June 22, 2013

Embedding an IPython shell in the middle of python code

For a long time I had been looking for a satisfactory way to export a variable from some python code to the main python workspace. This stackoverflow thread sort of got at it, but not really. I love pdb, especially pdb.pm(), however, sometimes you just need the full capability of IPython, including object introspection.

Well, there is an amazing solution. In the place in the code where you would do
import pdb;pdb.set_trace()
, if you do instead
from IPython import embed; embed()
, your code will pause at that place and you will get a fully functional IPython shell. When you exit from that shell you code will continue as usual.

No comments:

Post a Comment