为什么readline.read_history_file给我'IOError:[Errno 2]没有这样的文件或目录'

Man*_*Dee 3 python readline python-2.7

我的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()
Run Code Online (Sandbox Code Playgroud)

我正在使用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
Run Code Online (Sandbox Code Playgroud)

该文件是可读写的:

[johndoe@here]# ls -l ~/.pyhistory
-rw-------  1 johndoe  somegroup  325 21 Sep  2012 /Users/johndoe/.pyhistory
Run Code Online (Sandbox Code Playgroud)

什么可能导致这个问题?

Man*_*Dee 19

您的历史记录文件似乎是旧版本.尝试将其转换为更高版本的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()
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,我相信E做到了!也许ManicDee是理想主义,一元论/标志主义或现代主义等哲学学派的订阅者,因此认为过去的ManicDee(问题提问者)是一个无关的实体(毕竟,回答这个问题的ManicDee是显然是一个不同的实体:E有答案,另一个ManicDee没有).虽然我也认为E可能是一个角色扮演者,但在这个场景中扮演"提问者"和"回答者"的角色,在单个场景中有两个截然不同的角色. (3认同)
  • 我刚碰到这个.令人讨厌的是错误是"没有这样的文件或目录",而且这是一个IOError.它应该类似于readline.FileParseError.并且消息应该类似于"历史文件中的预期版本2标题" (2认同)
  • 我刚刚发现这是GNU的readline和BSD的libedit的问题.我有一个virtualenv,其readline是针对libedit编译的.它是libedit,期望有趣的标题和\ 040而不是空格.我的系统范围的自制python使用的readline是根据GNU的readline编译的,它编写的历史文件没有标题和常规空格.我通过删除virtualenv并重新创建它来解决问题,以便它指向与系统python相同的readline.so. (2认同)
  • 你,呃,只是以第二人称回答你自己的问题吗? (2认同)