Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch an IPython shell on exception

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.

like image 240
levesque Avatar asked Nov 20 '10 19:11

levesque


People also ask

How do I start IPython shell?

You start IPython by typing “ipython” in your terminal.

How do I run IPython from command prompt?

Install IPython The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd , shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.

How do I run an IPython code?

If you want some code to be run at the beginning of every IPython session, the easiest way is to add Python (. py) or IPython (. ipy) scripts to your profile_default/startup/ directory. Files here will be executed as soon as the IPython shell is constructed, before any other code or scripts you have specified.

What is the IPython shell?

IPython (Interactive Python) is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history.


2 Answers

Update for IPython v0.13:

import sys from IPython.core import ultratb sys.excepthook = ultratb.FormattedTB(mode='Verbose',      color_scheme='Linux', call_pdb=1) 
like image 164
Adam Greenhall Avatar answered Oct 06 '22 18:10

Adam Greenhall


Doing:

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

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

like image 34
rcoup Avatar answered Oct 06 '22 18:10

rcoup