IPython 中的嵌入式交互式 shell

发布于 2024-10-28 11:57:01 字数 1139 浏览 3 评论 0 原文

在切换到 IPython v0.11(使用 Python 2.6.1)之前,可以使用例如 这个,例如

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

“嵌入式 shell 已被重构为一个真正独立的 InteractiveShell 子类,称为 InteractiveShellEmbed em>。所有嵌入逻辑已从基类中取出并放入嵌入子类中”(请参阅​​此处此处 )。

按照我的理解,您现在应该能够简单地启动控制台

import IPython
IPython.embed()

但是,这会引发

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

如果我们为 exit_msg 传递一个字符串,那么

IPython.embed(exit_msg='Whatever')

它会引发不同的错误

AttributeError:“InteractiveShellEmbed”对象没有属性“set_completer”

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甲如呢乙后呢 2024-11-04 11:57:01

如今(3.0+)您需要做的就是:

from IPython import embed; embed()

如果您的意思是在 IPython 中嵌入另一个 IPython shell(递归地),那么很长一段时间以来都不支持这一点,但这个问题在去年得到了修补。

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.

涙—继续流 2024-11-04 11:57:01

具体github wiki 上的说明

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文