Launch an IPython shell on exception

PythonDebuggingIpython

Python Problem Overview


Is there a way to launch an IPython shell or prompt when my program runs a line that raises an exception?

I'm mostly interested in the context, variables, in the scope (and subscopes) where the exception was raised. Something like Visual Studio's debugging, when an exception is thrown but not caught by anyone, Visual Studio will halt and give me the call stack and the variables present at every level.

Do you think there's a way to get something similar using IPython?

EDIT: The -pdb option when launching IPython doesn't seem do what I want (or maybe I don't know how to use it properly, which is entirely possible). I run the following script :

def func():
    z = 2
    g = 'b'
    raise NameError("This error will not be caught, but IPython still"
                    "won't summon pdb, and I won't be able to consult"
                    "the z or g variables.")
    
x = 1
y = 'a'

func()

Using the command :

ipython -pdb exceptionTest.py

Which stops execution when the error is raised, but brings me an IPython prompt where I have access to the global variables of the script, but not the local variables of function func. pdb is only invoked when I directly type a command in ipython that causes an error, i.e. raise NameError("This, sent from the IPython prompt, will trigger pdb.").

I don't necessarily need to use pdb, I'd just like to have access to the variables inside func.

EDIT 2: It has been a while, IPython's -pdb option is now working just as I want it to. That means when I raise an exception I can go back in the scope of func and read its variables z and g without any problem. Even without setting the -pdb option, one can run IPython in interactive mode then call the magic function %debug after the program has exit with error -- that will also drop you into an interactive ipdb prompt with all scopes accessibles.

Python Solutions


Solution 1 - Python

Update for IPython v0.13:

import sys
from IPython.core import ultratb
sys.excepthook = ultratb.FormattedTB(mode='Verbose',
     color_scheme='Linux', call_pdb=1)

Solution 2 - Python

Doing:

ipython --pdb -c "%run exceptionTest.py"

kicks off the script after IPython initialises and you get dropped into the normal IPython+pdb environment.

Solution 3 - Python

You can try this:

from ipdb import launch_ipdb_on_exception

def main():
    with launch_ipdb_on_exception():
        # The rest of the code goes here.
        [...]

Solution 4 - Python

[ipdb][1] integrates IPython features into pdb. I use the following code to throw my apps into the IPython debugger after an unhanded exception.

import sys, ipdb, traceback

def info(type, value, tb):
    traceback.print_exception(type, value, tb)
    ipdb.pm()

sys.excepthook = info

[1]: http://pypi.python.org/pypi/ipdb "ipdb"

Solution 5 - Python

@snapshoe's answer does not work on newer versions of IPython.

This does however:

import sys 
from IPython import embed

def excepthook(type, value, traceback):
    embed()

sys.excepthook = excepthook

Solution 6 - Python

You can do something like the following:

import sys
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()

def excepthook(type, value, traceback):
    ipshell()

sys.excepthook = excepthook

See sys.excepthook and Embedding IPython.

Solution 7 - Python

@Adam's works like a charm except that IPython loads a bit slowly(800ms on my machine). Here I have a trick to make the load lazy.

class ExceptionHook:
    instance = None

    def __call__(self, *args, **kwargs):
        if self.instance is None:
            from IPython.core import ultratb
            self.instance = ultratb.FormattedTB(mode='Verbose',
                 color_scheme='Linux', call_pdb=1)
        return self.instance(*args, **kwargs)
sys.excepthook = ExceptionHook()

Now we don't need to wait at the very beginning. Only when the program crashes will cause IPython to be imported.

Solution 8 - Python

This man page says iPython has --[no]pdb option to be passed at command line to start iPython for uncaught exceptions. Are you looking for more?

EDIT: python -m pdb pythonscript.py can launch pdb. Not sure about similar thing with iPython though. If you are looking for the stack trace and general post-mortem of the abnormal exit of program, this should work.

Solution 9 - Python

If you want to both get the traceback and open a IPython shell with the environment at the point of the exception:

def exceptHook(*args):
    '''A routine to be called when an exception occurs. It prints the traceback
    with fancy formatting and then calls an IPython shell with the environment
    of the exception location.
    '''
    from IPython.core import ultratb
    ultratb.FormattedTB(call_pdb=False,color_scheme='LightBG')(*args)
    from IPython.terminal.embed import InteractiveShellEmbed
    import inspect
    frame = inspect.getinnerframes(args[2])[-1][0]
    msg   = 'Entering IPython console at {0.f_code.co_filename} at line {0.f_lineno}'.format(frame)
    savehook = sys.excepthook # save the exception hook
    InteractiveShellEmbed()(msg,local_ns=frame.f_locals,global_ns=frame.f_globals)
    sys.excepthook = savehook # reset IPython's change to the exception hook

import sys
sys.excepthook = exceptHook

Note that it is necessary to pull than namespace information from the last frame referenced by the traceback (arg[2])

Solution 10 - Python

Do you actually want to open a pdb session at every exception point? (as I think a pdb session opened from ipython is the same as the one open in the normal shell). If that's the case, here's the trick: http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlevesqueView Question on Stackoverflow
Solution 1 - PythonAdam GreenhallView Answer on Stackoverflow
Solution 2 - PythonrcoupView Answer on Stackoverflow
Solution 3 - PythonSardathrion - against SE abuseView Answer on Stackoverflow
Solution 4 - PythonjonView Answer on Stackoverflow
Solution 5 - PythonbcoughlanView Answer on Stackoverflow
Solution 6 - PythonsnapshoeView Answer on Stackoverflow
Solution 7 - PythonRayView Answer on Stackoverflow
Solution 8 - Pythonvpit3833View Answer on Stackoverflow
Solution 9 - PythonbhtView Answer on Stackoverflow
Solution 10 - PythondirksenView Answer on Stackoverflow