【问题标题】:starting IPython using Python code使用 Python 代码启动 IPython
【发布时间】:2015-02-04 23:34:22
【问题描述】:

我在 Windows 上的 Python 2.7 shell 中,因为没有制表符完成和? 文档我会发疯,我想从那里启动一个 IPython shell。

由于各种(非常乏味)的原因,我无法通过Scripts\ipython.exe 直接启动 IPython。我只想要将我带到 IPython 的纯 Python 代码。我已经搜索并找到了以下内容:

from IPython.terminal.ipapp import TerminalIPythonApp as t;
t.instance().initialize()

from IPython.terminal.ipapp import TerminalInteractiveShell as t;
t.instance()

...在这两种情况下,我最终都进入了某种 IPython shell,但在这两种情况下,这都不是我认为的“正常”IPython 会话:例如,它缺少颜色,缺少 [1] next到InOut,并且缺乏使用??? 来查看文档字符串的能力。

有没有办法从 Python 中获得“正常”外观和行为的 IPython shell,而不必从 Python 中出来并直接从 .exe 启动?原则上,我想一定有一种纯 Python(因此是跨平台的,甚至不是特定于 Windows 的)的方法,但我在谷歌上搜索试图找到它。

【问题讨论】:

  • “繁琐的原因”与这样一个事实有关,因为第三方包依赖地狱,我有多个 Python 发行版:一些有IPython<=0.13,一些更现代。两个是 Python 2.7.x,但一个是 3.2,一个是 2.5.4(呃)。甚至缺少setuptools 并且(出于更乏味的原因)我想保持这种状态。在我的 PsychoPy 发行版(Python 2.7.3,带有 setuptools)上,ipython.exe 只是默默地失败了。

标签: python shell ipython


【解决方案1】:

这是来自 linux 中的 ipython 启动脚本。

from IPython import start_ipython
start_ipython()

【讨论】:

  • 看起来这确实是最好和最“IPythonic”的答案,只要一个包的版本足够现代。对我来说并非总是如此,但我没有在问题中告诉你,你的回答让我走上了正确的轨道来覆盖我的其他基地——谢谢!
【解决方案2】:

按照Scripts\ipython-script.py 的引导并查看pkg_resources.load_entry_point 在各种情况下返回的内容,我最终得到了以下脚本,它似乎涵盖了我的所有基础:

import sys
import IPython
try:
    from IPython import start_ipython as entry_point
except ImportError:
    try:
        from IPython.terminal.ipapp import launch_new_instance as entry_point
    except ImportError:
        try:
            from IPython.frontend.terminal.ipapp import launch_new_instance as entry_point
        except ImportError:
            try:
                from pkg_resources import load_entry_point
                entry_point = load_entry_point( 'ipython', 'console_scripts', 'ipython' )
            except ImportError:
                try:
                    from pkg_resources import run_script
                    def entry_point(): run_script( 'ipython', 'ipython' ); return 0
                except ImportError:
                    raise ImportError( 'run out of options for launching IPython version %s under Python version %s' % ( IPython.__version__, sys.version ) )
sys.exit( entry_point() )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多