Alain
Alain

Reputation: 5905

Embedded interactive shell in IPython

Before switching to IPython v0.11 (using Python 2.6.1), it was possible to embed an interactive IPython shell using for example this, e.g.

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell() # this call anywhere in your program will start IPython

"The embedded shell has been refactored into a truly standalone subclass of InteractiveShell called InteractiveShellEmbed. All embedding logic has been taken out of the base class and put into the embedded subclass" (see here and here).

The way I understand it you should now be able to simply start a console by

import IPython
IPython.embed()

However, this raises

TraitError: The 'exit_msg' trait of an InteractiveShellEmbed instance must be a string, but a value of u'' was specified.

If we pass a string for exit_msg by

IPython.embed(exit_msg='Whatever')

Then it raises a different error

AttributeError: 'InteractiveShellEmbed' object has no attribute 'set_completer'

Did anybody else encounter this problem? Otherwise this might be a bug since it is a developer version after all.

Upvotes: 7

Views: 12655

Answers (2)

Jason Newton
Jason Newton

Reputation: 1211

These days (3.0+) all you need to do is:

from IPython import embed; embed()

If you mean embedding another IPython shell in IPython (recursively), there was a long time that this was not supported, but that problem was patched last year.

Upvotes: 10

Jonathan Hendler
Jonathan Hendler

Reputation: 1259

There are specific instructions on the github wiki:

from IPython.frontend.terminal.ipapp import TerminalIPythonApp
app = TerminalIPythonApp.instance()
app.initialize(argv=[]) # argv=[] instructs IPython to ignore sys.argv
app.start()

Upvotes: 3

Related Questions