Note that there are some explanatory texts on larger screens.

plurals
  1. POLaunch an IPython shell on exception
    primarykey
    data
    text
    <p>Is there a way to launch an IPython shell or prompt when my program runs a line that raises an exception?</p> <p>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.</p> <p>Do you think there's a way to get something similar using IPython?</p> <p><strong>EDIT:</strong> The <code>-pdb</code> 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 : </p> <pre><code>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() </code></pre> <p>Using the command : </p> <pre><code>ipython -pdb exceptionTest.py </code></pre> <p>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. <code>pdb</code> is only invoked when I directly type a command in ipython that causes an error, i.e. <code>raise NameError("This, sent from the IPython prompt, will trigger pdb.")</code>.</p> <p>I don't necessarily need to use <code>pdb</code>, I'd just like to have access to the variables inside <code>func</code>.</p> <p><strong>EDIT 2:</strong> It has been a while, IPython's <code>-pdb</code> 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 <code>func</code> and read its variables <code>z</code> and <code>g</code> without any problem. Even without setting the <code>-pdb</code> option, one can run IPython in interactive mode then call the magic function <code>%debug</code> after the program has exit with error -- that will also drop you into an interactive ipdb prompt with all scopes accessibles. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload