在IPython中有一個%kill_embedded
命令。
它不會讓您直接回到shell提示符,但會跳過其他嵌入實例。
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
而這裏的輸出:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD(2016年6月3日):似乎%kill_embedded
功能在IPython的4.0種破裂;你可以使用%exit_raise
這會引發異常並返回到shell。
跳過其他嵌入實例正是我想要的,謝謝。 – 2015-02-11 11:40:47
@ MikeH-R不客氣! – 2015-02-11 11:49:44
你最後一次輸入是做什麼的,只是一個普通的CTRL + D?我試圖在IPython 4.0中重現這個例子,但我總是必須經歷循環中的所有「嵌入」。看起來像'%kill_embedded'實際上沒有效果。 – bluenote10 2016-02-29 14:53:32