Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 779
Alain Avatar asked Mar 28 '11 23:03

Alain


2 Answers

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.

like image 94
Jason Newton Avatar answered Sep 19 '22 02:09

Jason Newton


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()
like image 39
Jonathan Hendler Avatar answered Sep 19 '22 02:09

Jonathan Hendler