【问题标题】:Unable to set IPython prompt无法设置 IPython 提示
【发布时间】:2017-02-08 08:24:06
【问题描述】:

我正在使用以下命令运行 ipython:

c:\python27\scripts\ipython

出于某些原因,我正在尝试恢复旧的 Python 提示行为 (">>>")。

为此,我尝试在互联网上进行广泛搜索,但无济于事。

然后我找到了 IPython 文档,结果发现它令人困惑且无济于事。 根据http://ipython.readthedocs.io/en/stable/config/details.html

要设置新提示,请将其分配给 IPython shell 的 prompts 属性:

In [2]: ip = get_ipython()
...: ip.prompts = MyPrompt(ip)

/home/bob >>> # it works

我得到了 get_ipython 未定义的异常:

[TerminalIPythonApp] ERROR | Exception while loading config file C:\Users\xxx\.ipython\profile_default\ipython_config.py
Traceback (most recent call last):
File "c:\python27\lib\site-packages\traitlets\config\application.py", line 562, in _load_config_files
    config = loader.load_config()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 457, in load_config
    self._read_file_as_dict()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 489, in _read_file_as_dict
    py3compat.execfile(conf_filename, namespace)
  File "c:\python27\lib\site-packages\ipython_genutils\py3compat.py", line 278, in execfile
    exec(compiler(scripttext, filename, 'exec'), glob, loc)
  File "C:\Users\rgomulk\.ipython\profile_default\ipython_config.py", line 9, in <module>
    ip = get_ipython()
NameError: name 'get_ipython' is not defined
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

(在我的 ipython_config.py 中有以下完整代码:

from IPython.terminal.prompts import Prompts, Token

class MyPrompt(Prompts):
    def in_prompt_tokens(self, cli=None):
        return [(Token.Prompt, ' >>>')]

ip = get_ipython()
ip.prompts = MyPrompt(ip)

在下一轮谷歌搜索之后,我在配置中添加了以下行:

from IPython import get_ipython

这一次结果不一样:

[TerminalIPythonApp] ERROR | Exception while loading config file C:\Users\xxx\.ipython\profile_default\ipython_config.py
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\traitlets\config\application.py", line 562, in _load_config_files
    config = loader.load_config()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 457, in load_config
    self._read_file_as_dict()
  File "c:\python27\lib\site-packages\traitlets\config\loader.py", line 489, in _read_file_as_dict
    py3compat.execfile(conf_filename, namespace)
  File "c:\python27\lib\site-packages\ipython_genutils\py3compat.py", line 278, in execfile
    exec(compiler(scripttext, filename, 'exec'), glob, loc)
  File "C:\Users\rgomulk\.ipython\profile_default\ipython_config.py", line 11, in <module>
    ip.prompts = MyPrompt(ip)
AttributeError: 'NoneType' object has no attribute 'prompts'

所以问题是双重的: 1. 如何实际设置提示/恢复旧提示行为? 2. 为什么 IPython 文档中的代码不起作用?这是实现或文档中的错误吗?

IPython 版本和 IPython 输出中已经给出的其他版本。

问候, 罗伯特

【问题讨论】:

    标签: ipython


    【解决方案1】:

    另一种可能性 - 不太通用 - 是使用预定义的 Prompt 类:

    from IPython.terminal.prompts import ClassicPrompts
    
    c = get_config()
    
    c.TerminalInteractiveShell.prompts_class = ClassicPrompts
    

    【讨论】:

      【解决方案2】:

      经过大量搜索(真的很乏味,其他人也很困惑,尤其是区分启动脚本和配置脚本)我找到了这个页面:Jupyter prompts

      这导致了(工作)解决方案:

      from IPython.terminal.prompts import Prompts
      from pygments.token import Token
      
      class MyPrompt(Prompts):
          def in_prompt_tokens(self, cli=None):
              return [(Token.Prompt, '>>> ')]
      
      c.TerminalInteractiveShell.prompts_class = MyPrompt
      

      (请注意官方文档Official IPython docs似乎没有要求:

      文件通常从获取根配置对象开始:

      c = get_config()

      问候,

      罗伯特

      【讨论】:

      • 请注意,此方法不再有效,iPython 不久前更改了他们的配置处理
      • 最后一行现在应该是c.InteractiveShell.prompts_class = MyPrompt`
      猜你喜欢
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      相关资源
      最近更新 更多