【问题标题】:Why does readline.read_history_file give me 'IOError: [Errno 2] No such file or directory'为什么 readline.read_history_file 给我 'IOError: [Errno 2] No such file or directory'
【发布时间】:2013-07-23 09:23:08
【问题描述】:

我的 Python 历史文件位于 ~/.pyhistory 并包含以下内容:

from project.stuff import *
quit()
from project.stuff import *
my_thing = Thing.objects.get(id=21025)
my_thing
my_thing.child_set.all()
my_thing.current_state
my_thing.summary_set
my_thing.summary_set.all()
[ x.type for x in my_thing.child_set.all() ]
[ x.type for x in my_thing.child_set.all().order_by( 'datesubmitted' ) ]
quit()

我正在使用 virtualenv 和 virtualenvwrapper 来构建虚拟环境。今天我遇到了 readline 没有在我的历史文件中读取的问题:

>>> historyPath
'/Users/johndoe/.pyhistory'
>>> readline.read_history_file(historyPath)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory

我可以读写该文件:

[johndoe@here]# ls -l ~/.pyhistory
-rw-------  1 johndoe  somegroup  325 21 Sep  2012 /Users/johndoe/.pyhistory

什么可能导致这个问题?

【问题讨论】:

    标签: python python-2.7 readline


    【解决方案1】:

    您的历史文件似乎是旧版本。尝试将其转换为更高版本的 readline 所期望的格式,最值得注意的是第一行应该是字面上的 '_HiStOrY_V2_' 并且所有空格都应该替换为 '\040':

    _HiStOrY_V2_
    from\040project.stuff\040import\040*
    quit()
    from\040project.stuff\040import\040*
    my_thing\040=\040Thing.objects.get(id=21025)
    my_thing
    my_thing.child_set.all()
    my_thing.current_state
    my_thing.summary_set
    my_thing.summary_set.all()
    [\040x.type\040for\040x\040in\040my_thing.child_set.all()\040]
    [\040x.type\040for\040x\040in\040my_thing.child_set.all().order_by(\040'datesubmitted'\040)\040]
    quit()
    

    我不确定这是底层 readline/libedit 库还是 Python readline 模块的怪癖,但这对我有用。

    【讨论】:

    • 在将 osx 升级到 Mavericks 后遇到了这个问题。 readline 版本必须已更新或其他内容。谢谢。
    • 我刚碰到这个。错误是“没有这样的文件或目录”,这是一个 IOError,这太烦人了。它应该类似于 readline.FileParseError。并且消息应该类似于“历史文件中的预期版本 2 标头”
    • 我刚刚发现这是 GNU 的 readline 与 BSD 的 libedit 的问题。我有一个 virtualenv,它的 readline 是针对 libedit 编译的。 libedit 需要有趣的标题和 \040 而不是空格。我的系统范围的自制 python 使用的 readline 是针对 GNU 的 readline 编译的,它编写了没有标题和常规空格的历史文件。我通过删除 virtualenv 并重新创建它来解决问题,以便它指向与系统 python 相同的 readline.so。
    • 你,呃,只是用第二个人回答你自己的问题吗..?
    • 是的,我相信 E 做到了!也许 ManicDee 是理想主义、一元论/逻辑主义或现在主义等哲学学派的订阅者,因此认为过去的 ManicDee(提问者)是一个不相关的实体(毕竟,回答问题的 ManicDee 是显然是一个不同的实体:E 有答案,另一个 ManicDee 没有)。虽然我也想到 E 可能是一个角色扮演者,但在这个场景中扮演“提问者”和“回答者”的角色,两个不同的角色在一个场景中。
    猜你喜欢
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多