Is it possible to go into ipython from code?

23,568

Solution 1

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)

Solution 2

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

Your program might look like this

In [5]: cat > tmpf.py
a = 1

from IPython import embed
embed() # drop into an IPython session.
        # Any variables you define or modify here
        # will not affect program execution

c = 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.py
Python 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]: who
a       embed

In [2]: a
Out[2]: 1

In [3]:
Do you really want to exit ([y]/n)? y


In [7]: who
a       c       embed

Solution 3

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

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

But it's probably better to just use ipdb

Solution 4

The equivalent of

import pdb; pdb.set_trace()

with IPython is something like:

from IPython.ipapi import make_session; make_session()
from IPython.Debugger import Pdb; Pdb().set_trace()

It's a bit verbose, but good to know if you don't have ipdb installed. The make_session call is required once to set up the color scheme, etc, and set_trace calls can be placed anywhere you need to break.

Solution 5

I like to simply paste this one-liner in my scripts where I want to set a breakpoint:

__import__('IPython').Debugger.Pdb(color_scheme='Linux').set_trace()

Newer version might use:

__import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace()
Share:
23,568

Related videos on Youtube

MarkD
Author by

MarkD

Updated on January 19, 2020

Comments

  • MarkD
    MarkD over 4 years

    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?

  • MarkD
    MarkD almost 15 years
    so there's no way of writing something like ipython.set_trace() ? :)
  • Xiong Chiamiov
    Xiong Chiamiov over 14 years
    Works amazingly well with Django. Well, except the fact that I can't see the text I'm typing, but that's probably easily fixible (since ipdb's only six lines).
  • Jeremy Dunck
    Jeremy Dunck over 12 years
    Actually, the issue there is that Django forks a separate thread for the runserver, and any time you make a code edit, it respawns the thread. This normally works fine, but if you're sitting in pdb when the thread gets killed, the terminal goes insane. You can fix this by breaking out of runserver, runniny 'stty sane', then starting runserver again.
  • joon
    joon about 12 years
    This dropping into an IPython session is very cool. Thank you so much!
  • gerrit
    gerrit about 10 years
    I foolishly placed this inside an (almost infinite) loop. How can I ever get out?
  • confused00
    confused00 over 9 years
    @gerrit, press Ctrl + d if you are still in there.
  • gerrit
    gerrit about 9 years
    @confused00 That would have been mightily infinite, 6 months later ;-)
  • ostrokach
    ostrokach almost 9 years
    ImportError: No module named 'IPython.Debugger' on python 3.4 / IPython 3
  • alisianoi
    alisianoi almost 9 years
    Thank you! A bit simpler: ipython --pdb -- ./path/to/my/script --with-args here
  • Xiong Chiamiov
    Xiong Chiamiov about 8 years
    One advantage this has over pdb and ipdb is that any exceptions caused by the code you run get full tracebacks (because you aren't already "in" one). This is super useful for me, since I have a tendency to use pdb as a way to set up a bunch of variables for an interactive shell.
  • SebMa
    SebMa almost 6 years
    @daniel-roseman Hi, I get ModuleNotFoundError: No module named 'IPython.Debugger', ModuleNotFoundError: No module named 'IPython.Shell' and ImportError: cannot import name 'ipapi'
  • SebMa
    SebMa almost 6 years
    @memoselyk Hi, I get ModuleNotFoundError: No module named 'IPython.Debugger', ModuleNotFoundError: No module named 'IPython.Shell' and ImportError: cannot import name 'ipapi'
  • Jim K.
    Jim K. over 5 years
    This is the right answer folks. ipdb is great, but it's not even close to the same UX as ipython.
  • MatrixManAtYrService
    MatrixManAtYrService over 5 years
    @gerrit, If you're stuck in an embed loop: def f() : pass ; IPython.embed = f so that the embed() call is a noop, then Ctrl + D
  • gerrit
    gerrit over 5 years
    There is a way to get the breakpoint() function to get into ipdb.