启动ipython出错_python-在异常情况下启动IPython shell
python-在異常情況下啟動IPython shell
當我的程序運行引發異常的行時,是否可以啟動IPython Shell或提示?
我對引發異常的上下文,變量,范圍(和子范圍)最感興趣。 類似于Visual Studio的調試,當引發異常但未被任何人捕獲時,Visual Studio將停止并為我提供調用堆棧以及每個級別存在的變量。
您是否認為有辦法使用IPython獲得類似的東西?
編輯:啟動IPython時的-pdb選項似乎并沒有達到我想要的功能(或者也許我不知道如何正確使用它,這完全有可能)。 我運行以下腳本:
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
當出現錯誤時,它將停止執行,但是會給我一個IPython提示,在這里我可以訪問腳本的全局變量,但不能訪問函數func的局部變量。 僅當我在ipython中直接鍵入會導致錯誤的命令時(即func)才調用-pdb。
我不一定需要使用-pdb,我只想訪問func中的變量。
編輯2:已經有一段時間了,IPython的-pdb選項現在可以按照我的意愿工作。 這意味著當我引發異常時,我可以返回func的范圍,并毫無問題地讀取其變量z和g。 即使沒有設置-pdb選項,也可以在交互模式下運行IPython,然后在程序錯誤退出后調用魔術函數%debug-這也會使您進入具有所有作用域的交互式ipdb提示符。
10個解決方案
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初始化之后啟動腳本,您將進入正常的IPython + pdb環境。
rcoup answered 2020-06-16T16:46:19Z
12 votes
ipdb將IPython功能集成到pdb中。 在發生異常處理后,我使用以下代碼將應用程序扔到IPython調試器中。
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
您可以執行以下操作:
import sys
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
def excepthook(type, value, traceback):
ipshell()
sys.excepthook = excepthook
請參閱sys.excepthook和嵌入IPython。
snapshoe answered 2020-06-16T16:47:49Z
3 votes
@Adam的工作就像一個魅力,除了IPython加載緩慢(在我的計算機上為800ms)之外。 在這里,我有一個技巧可以使負載變懶。
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()
現在我們不需要一開始就等待。 僅當程序崩潰時,才會導入IPython。
Ray answered 2020-06-16T16:48:14Z
2 votes
該手冊頁說,iPython具有python -m pdb pythonscript.py選項,該選項將在命令行中傳遞,以啟動iPython捕獲未捕獲的異常。 您是否在尋找更多?
編輯:python -m pdb pythonscript.py可以啟動pdb。 雖然不確定與iPython類似。 如果您正在尋找程序異常退出的堆棧跟蹤和常規驗尸,這應該可以工作。
vpit3833 answered 2020-06-16T16:48:40Z
1 votes
您是否真的想在每個異常點打開一個pdb會話? (因為我認為從ipython打開的pdb會話與在普通shell中打開的會話相同)。 如果真是這樣,這是竅門:[http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/]
dirksen answered 2020-06-16T16:49:00Z
1 votes
如果您既要獲取回溯信息,又要在異常發生時打開帶有環境的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
請注意,有必要從回溯引用的最后一幀(arg [2])中提取名稱空間信息。
bht answered 2020-06-16T16:49:24Z
總結
以上是生活随笔為你收集整理的启动ipython出错_python-在异常情况下启动IPython shell的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pandas python csv_py
- 下一篇: python从零开始进阶_从零开始学Py