This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.

If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).

Module: terminal.embed

An embedded IPython shell.

3 Classes

class IPython.terminal.embed.KillEmbedded

Bases: Exception

class IPython.terminal.embed.EmbeddedMagics(shell=None, **kwargs)

Bases: IPython.core.magic.Magics


%exit_raise Make the current embedded kernel exit and raise and exception.

This function sets an internal flag so that an embedded IPython will raise a IPython.terminal.embed.KillEmbedded Exception on exit, and then exit the current I. This is useful to permanently exit a loop that create IPython embed instance.

%kill_embedded [-i] [-x] [-y]

%kill_embedded : deactivate for good the current embedded IPython

This function (after asking for confirmation) sets an internal flag so that an embedded IPython will never activate again for the given call location. This is useful to permanently disable a shell that is being called inside a loop: once you’ve figured out what you needed from it, you may then kill it and the program will then continue to run without the interactive shell interfering again.

Kill Instance Option:

If for some reasons you need to kill the location where the instance is created and not called, for example if you create a single instance in one place and debug in many locations, you can use the --instance option to kill this specific instance. Like for the call location killing an “instance” should work even if it is recreated within a loop.


This was the default behavior before IPython 5.2

optional arguments:
-i, --instance Kill instance instead of call location
-x, --exit Also exit the current session
-y, --yes Do not ask confirmation
class IPython.terminal.embed.InteractiveShellEmbed(**kw)

Bases: IPython.terminal.interactiveshell.TerminalInteractiveShell


Create a configurable given a config config.

  • config (Config) – If this is empty, default values are used. If config is a Config instance, it will be used to configure the instance.
  • parent (Configurable instance, optional) – The parent Configurable instance of this object.


Subclasses of Configurable must call the __init__() method of Configurable before doing anything else and using super():

class MyConfigurable(Configurable):
    def __init__(self, config=None):
        super(MyConfigurable, self).__init__(config=config)
        # Then any other code you need to finish initialization.

This ensures that instances will be configured properly.


Explicitly overwrite IPython.core.interactiveshell to do nothing.

mainloop(local_ns=None, module=None, stack_depth=0, display_banner=None, global_ns=None, compile_flags=None)

Embeds IPython into a running python program.

  • module (local_ns,) – Working local namespace (a dict) and module (a module or similar object). If given as None, they are automatically taken from the scope where the shell was called, so that program variables become visible.
  • stack_depth (int) – How many levels in the stack to go to looking for namespaces (when local_ns or module is None). This allows an intermediate caller to make sure that this function gets the namespace from the intended level in the stack. By default (0) it will get its locals and globals from the immediate caller.
  • compile_flags – A bit field identifying the __future__ features that are enabled, as passed to the builtin compile() function. If given as None, they are automatically taken from the scope where the shell was called.

1 Function


Call this to embed IPython at the current point in your program.

The first invocation of this will create an InteractiveShellEmbed instance and then call it. Consecutive calls just call the already created instance.

If you don’t want the kernel to initialize the namespace from the scope of the surrounding function, and/or you want to load full IPython configuration, you probably want IPython.start_ipython() instead.

Here is a simple example:

from IPython import embed
a = 10
b = 20
embed(header='First time')
c = 30
d = 40

Full customization can be done by passing a Config in as the config argument.