3

我的 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

什么可能导致这个问题?

4

1 回答 1

22

您的历史文件似乎是旧版本。尝试将其转换为更高版本的 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 模块的怪癖,但这对我有用。

于 2013-07-24T03:50:03.660 回答