Is it possible to go into ipython from code?

Geo picture Geo · Jul 14, 2009 · Viewed 21.8k times · Source

For my debugging needs, pdb is pretty good. However, it would be much cooler (and helpful) if I could go into ipython. Is this thing possible?

Answer

Daniel Roseman picture Daniel Roseman · Jul 14, 2009

There is an ipdb project which embeds iPython into the standard pdb, so you can just do:

import ipdb; ipdb.set_trace()

It's installable via the usual pip install ipdb.

ipdb is pretty short, so instead of easy_installing you can also create a file ipdb.py somewhere on your Python path and paste the following into the file:

import sys
from IPython.Debugger import Pdb
from IPython.Shell import IPShell
from IPython import ipapi

shell = IPShell(argv=[''])

def set_trace():
    ip = ipapi.get()
    def_colors = ip.options.colors
    Pdb(def_colors).set_trace(sys._getframe().f_back)