启动一个异常的IPython shell [英] Launch an IPython shell on exception

查看:207
本文介绍了启动一个异常的IPython shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我最感兴趣的是上下文,变量在引发异常的范围(和子表)中。像Visual Studio的调试一样,当抛出异常但没有被任何人捕获时,Visual Studio将停止并给我调用堆栈和变量存在于每个级别。



你认为有一种办法可以使用IPython获得类似的东西吗?



编辑: -pdb 选项似乎不会做我想要的(或者也许我不知道如何正确使用它,这是完全可能的)。我运行以下脚本:

  def func():
z = 2
g ='b'
raise NameError(这个错误不会被捕获,但IPython仍然
不会召唤pdb,我将无法查询
的z或g变量。 )

x = 1
y ='a'

func()

使用命令:

  ipython -pdb exceptionTest.py 

当错误发生时,哪个停止执行,但是在我可以访问脚本的全局变量的同时给我一个IPython提示,但不是函数func的局部变量。只有当我直接在ipython中键入一个导致错误的命令时,才调用 pdb ,即 raise NameError(从IPython提示符发送触发pdb)



我不一定需要使用 pdb ,我只想访问 func 中的变量。



编辑2: / strong>已经有一段时间了,IPython的 -pdb 选项现在正好像我想要的那样工作。这意味着当我提出异常时,我可以回到 func 的范围,并读取其变量 z g 没有任何问题。即使没有设置 -pdb 选项,可以以交互模式运行IPython,然后调用魔术函数%debug 程序具有错误的退出 - 这也将使您进入具有所有示波器可访问性的交互式ipdb提示符。

解决方案

更新 IPython v0.13

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


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.

解决方案

Update for IPython v0.13:

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

这篇关于启动一个异常的IPython shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆