gpt4 book ai didi

ipython - 无法设置 IPython 提示

转载 作者:行者123 更新时间:2023-12-02 17:21:49 29 4
gpt4 key购买 nike

我正在使用以下命令运行 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 输出中已经给出的其他版本。

问候,罗伯特

最佳答案

经过大量搜索(这真的很乏味,其他人也很困惑,尤其是在区分启动脚本和配置脚本方面)我找到了这个页面: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 :

The files typically start by getting the root config object:

c = get_config()

问候,

罗伯特

关于ipython - 无法设置 IPython 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42108005/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com