我可以让ipython退出调用代码吗? [英] Can I make ipython exit from the calling code?
问题描述
我有一些类似这样的代码:
I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
如果我随后使用该程序运行
If I then run this program with
python my_example_program.py
在循环的第一次迭代中,我进入了ipython shell,并可以根据需要检查item
和环境.
on the first iteration through the loop I get put into an ipython shell and can inspect item
and the environment as I would like to.
退出ipython时,循环继续,然后我可以按照您的期望检查下一个item
和环境.
On quitting ipython the loop resumes and then I can inspect the next item
and the environment as you would expect.
有没有办法让我从ipython退出此代码(以便我返回到shell提示符).除了打开另一个外壳并终止进程以外,还可以吗?
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
推荐答案
There's a %kill_embedded
command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
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(06.03.2016):似乎%kill_embedded
功能在IPython 4.0中已损坏;您可以使用%exit_raise
,这将引发异常并返回外壳.
UPD (06.03.2016): Seems that the %kill_embedded
feature is kind of broken in IPython 4.0; you can use %exit_raise
which will raise an exception and return back to the shell.
这篇关于我可以让ipython退出调用代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!