在IPython中嵌入式交互式shell

Ala*_*ain 6 python embed ipython

在切换到IPython v0.11(使用Python 2.6.1)之前,可以使用例如这样嵌入交互式IPython shell ,例如

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell() # this call anywhere in your program will start IPython
Run Code Online (Sandbox Code Playgroud)

"嵌入式shell已被重构为一个名为InteractiveShellEmbedInteractiveShell真正独立的子类.所有嵌入逻辑都已从基类中取出并放入嵌入式子类中"(参见此处此处).

我理解它的方式你现在应该可以简单地启动一个控制台

import IPython
IPython.embed()
Run Code Online (Sandbox Code Playgroud)

然而,这提高了

TraitError:InteractiveShellEmbed实例的'exit_msg'特征必须是字符串,但指定了u''的值.

如果我们通过一个字符串exit_msg通过

IPython.embed(exit_msg='Whatever')
Run Code Online (Sandbox Code Playgroud)

然后它引发了一个不同的错误

AttributeError:'InteractiveShellEmbed'对象没有属性'set_completer'

有没有人遇到过这个问题?否则这可能是一个错误,因为它毕竟是开发人员版本.

Jas*_*ton 9

这些天(3.0+)你需要做的就是:

from IPython import embed; embed()
Run Code Online (Sandbox Code Playgroud)

如果你的意思是在IPython中嵌入另一个IPython shell(递归地),很长一段时间不支持这个,但去年修补了这个问题.


Jon*_*ler 3

github wiki上有具体说明:

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