1

I'm using embed() from IPython (console), to interact with my script. Whenever I press CTRL+D it exits the interactive mode and proceeds to the next commends following the embed() call.

How do I abort the complete python script avoiding it from going to further commands after embed() from the interactive python?

What I have tried:

CTRL+C : only cancels my current command in the IPython

CTRL+D : leaves the IPython and proceeds to the next commands in my script

Typing exit() : same as CTRL+D

Here is a sample script:

#!/usr/bin/env python

from IPython import embed

print 'hello world'
embed()
print 'I dont want to reach here if I decide to quit from the IPython terminal!'
5
  • Are you speaking about iPython notebook? Or just the iPython console ? Commented Dec 12, 2016 at 5:55
  • I don't know about that. How do I check this? Commented Dec 12, 2016 at 5:57
  • @rkioki How are you launching iPython ? Commented Dec 12, 2016 at 5:57
  • @user312016 I simply make my script executable and type ./script.py. I use #!/usr/bin/env python in its header. Commented Dec 12, 2016 at 6:02
  • This answer on stackoverflow should help: stackoverflow.com/questions/28453165/… Commented Dec 12, 2016 at 6:03

1 Answer 1

4

One option is to nuke the process ID from the IPython session:

$ python test_embed.py
hello world
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, May 28 2015, 17:04:42)
Type "copyright", "credits" or "license" for more information.

IPython 5.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]: import os

In [2]: os.kill(os.getpid(), 9)
Killed: 9

EDIT: Looks like sending SIGQUIT with Ctrl-\ will also terminate the script entirely.

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

2 Comments

This definitely works, but I wonder if there is any easier shortcut.
`Ctrl-` is what I was looking for! Thanks perfect.

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.