无法设置IPython提示

时间:2017-02-08 08:24:07

标签: ipython

我正在使用以下内容运行ipython:

c:\python27\scripts\ipython

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

为了这个目的,我试图广泛搜索互联网,但没有用。

然后我找到了IPython文档,结果令人困惑且无益。 根据{{​​3}}

要设置新提示,请将其分配给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输出中已经提供的其他版本。

此致 罗伯特

2 个答案:

答案 0 :(得分: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()

此致

罗伯特

答案 1 :(得分:0)

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

from IPython.terminal.prompts import ClassicPrompts

c = get_config()

c.TerminalInteractiveShell.prompts_class = ClassicPrompts