13

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

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.

On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.

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?

1 Answer 1

24

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

And here's the output:

 $ 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): 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.

Sign up to request clarification or add additional context in comments.

5 Comments

skipping the other embed instances is exactly what I wanted, thank you.
What exactly did you do in the last input, just a regular CTRL+D? I'm trying to reproduce this example in IPython 4.0, but I always have to go through all embeds in the loop. Looks like %kill_embedded actually has no effect.
@bluenote10 Seems so :\ %exit_raise works in that case - it throws an exception and returns you right to the shell.
hm strange, I'm getting ERROR: Line magic function %exit_raise` not found.`
@bluenote10 That could have been because %exit_raise was added in 4.1 according to the issue. %kill_embedded works fine in IPython 5.1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.