日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

启动ipython出错_python-在异常情况下启动IPython shell

發(fā)布時(shí)間:2025/4/5 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 启动ipython出错_python-在异常情况下启动IPython shell 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python-在異常情況下啟動(dòng)IPython shell

當(dāng)我的程序運(yùn)行引發(fā)異常的行時(shí),是否可以啟動(dòng)IPython Shell或提示?

我對(duì)引發(fā)異常的上下文,變量,范圍(和子范圍)最感興趣。 類似于Visual Studio的調(diào)試,當(dāng)引發(fā)異常但未被任何人捕獲時(shí),Visual Studio將停止并為我提供調(diào)用堆棧以及每個(gè)級(jí)別存在的變量。

您是否認(rèn)為有辦法使用IPython獲得類似的東西?

編輯:啟動(dòng)IPython時(shí)的-pdb選項(xiàng)似乎并沒有達(dá)到我想要的功能(或者也許我不知道如何正確使用它,這完全有可能)。 我運(yùn)行以下腳本:

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()

使用命令:

ipython -pdb exceptionTest.py

當(dāng)出現(xiàn)錯(cuò)誤時(shí),它將停止執(zhí)行,但是會(huì)給我一個(gè)IPython提示,在這里我可以訪問腳本的全局變量,但不能訪問函數(shù)func的局部變量。 僅當(dāng)我在ipython中直接鍵入會(huì)導(dǎo)致錯(cuò)誤的命令時(shí)(即func)才調(diào)用-pdb。

我不一定需要使用-pdb,我只想訪問func中的變量。

編輯2:已經(jīng)有一段時(shí)間了,IPython的-pdb選項(xiàng)現(xiàn)在可以按照我的意愿工作。 這意味著當(dāng)我引發(fā)異常時(shí),我可以返回func的范圍,并毫無問題地讀取其變量z和g。 即使沒有設(shè)置-pdb選項(xiàng),也可以在交互模式下運(yùn)行IPython,然后在程序錯(cuò)誤退出后調(diào)用魔術(shù)函數(shù)%debug-這也會(huì)使您進(jìn)入具有所有作用域的交互式ipdb提示符。

10個(gè)解決方案

24 votes

IPython v0.13的更新:

import sys

from IPython.core import ultratb

sys.excepthook = ultratb.FormattedTB(mode='Verbose',

color_scheme='Linux', call_pdb=1)

Adam Greenhall answered 2020-06-16T16:45:55Z

19 votes

正在做:

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

在IPython初始化之后啟動(dòng)腳本,您將進(jìn)入正常的IPython + pdb環(huán)境。

rcoup answered 2020-06-16T16:46:19Z

12 votes

ipdb將IPython功能集成到pdb中。 在發(fā)生異常處理后,我使用以下代碼將應(yīng)用程序扔到IPython調(diào)試器中。

import sys, ipdb, traceback

def info(type, value, tb):

traceback.print_exception(type, value, tb)

ipdb.pm()

sys.excepthook = info

jon answered 2020-06-16T16:46:40Z

7 votes

@snapshoe的答案不適用于較新版本的IPython。

但是這樣做:

import sys

from IPython import embed

def excepthook(type, value, traceback):

embed()

sys.excepthook = excepthook

bcoughlan answered 2020-06-16T16:47:04Z

6 votes

您可以嘗試以下方法:

from ipdb import launch_ipdb_on_exception

def main():

with launch_ipdb_on_exception():

# The rest of the code goes here.

[...]

Sardathrion answered 2020-06-16T16:47:24Z

4 votes

您可以執(zhí)行以下操作:

import sys

from IPython.Shell import IPShellEmbed

ipshell = IPShellEmbed()

def excepthook(type, value, traceback):

ipshell()

sys.excepthook = excepthook

請(qǐng)參閱sys.excepthook和嵌入IPython。

snapshoe answered 2020-06-16T16:47:49Z

3 votes

@Adam的工作就像一個(gè)魅力,除了IPython加載緩慢(在我的計(jì)算機(jī)上為800ms)之外。 在這里,我有一個(gè)技巧可以使負(fù)載變懶。

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()

現(xiàn)在我們不需要一開始就等待。 僅當(dāng)程序崩潰時(shí),才會(huì)導(dǎo)入IPython。

Ray answered 2020-06-16T16:48:14Z

2 votes

該手冊(cè)頁說,iPython具有python -m pdb pythonscript.py選項(xiàng),該選項(xiàng)將在命令行中傳遞,以啟動(dòng)iPython捕獲未捕獲的異常。 您是否在尋找更多?

編輯:python -m pdb pythonscript.py可以啟動(dòng)pdb。 雖然不確定與iPython類似。 如果您正在尋找程序異常退出的堆棧跟蹤和常規(guī)驗(yàn)尸,這應(yīng)該可以工作。

vpit3833 answered 2020-06-16T16:48:40Z

1 votes

您是否真的想在每個(gè)異常點(diǎn)打開一個(gè)pdb會(huì)話? (因?yàn)槲艺J(rèn)為從ipython打開的pdb會(huì)話與在普通shell中打開的會(huì)話相同)。 如果真是這樣,這是竅門:[http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/]

dirksen answered 2020-06-16T16:49:00Z

1 votes

如果您既要獲取回溯信息,又要在異常發(fā)生時(shí)打開帶有環(huán)境的IPython Shell:

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

請(qǐng)注意,有必要從回溯引用的最后一幀(arg [2])中提取名稱空間信息。

bht answered 2020-06-16T16:49:24Z

總結(jié)

以上是生活随笔為你收集整理的启动ipython出错_python-在异常情况下启动IPython shell的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。