Is it possible to go into ipython from code? Is it possible to go into ipython from code? python python

Is it possible to go into ipython from code?


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 sysfrom IPython.Debugger import Pdbfrom IPython.Shell import IPShellfrom IPython import ipapishell = IPShell(argv=[''])def set_trace():    ip = ipapi.get()    def_colors = ip.options.colors    Pdb(def_colors).set_trace(sys._getframe().f_back)


In IPython 0.11, you can embed IPython directly in your code like this

Your program might look like this

In [5]: cat > tmpf.pya = 1from IPython import embedembed() # drop into an IPython session.        # Any variables you define or modify here        # will not affect program executionc = 2^D

This is what happens when you run it (I arbitrarily chose to run it inside an existing ipython session. Nesting ipython sessions like this in my experience can cause it to crash).

In [6]:In [6]: run tmpf.pyPython 2.7.2 (default, Aug 25 2011, 00:06:33)Type "copyright", "credits" or "license" for more information.IPython 0.11 -- An enhanced Interactive Python.?         -> Introduction and overview of IPython's features.%quickref -> Quick reference.help      -> Python's own help system.object?   -> Details about 'object', use 'object??' for extra details.In [1]: whoa       embedIn [2]: aOut[2]: 1In [3]:Do you really want to exit ([y]/n)? yIn [7]: whoa       c       embed


If you're using a more modern version of IPython (> 0.10.2) you can use something like

from IPython.core.debugger import PdbPdb().set_trace()

But it's probably better to just use ipdb