0

pip install --upgrade ipython最近执行了一次,因此将我的旧 ipython 版本更新为 0.13.2。从那时起,IPython 开始使用 sqlite 文件来存储其历史记录。

显然 $HOME/.ipython/history 中的旧历史文件完好无损,但未使用。相反profile_default,创建了一个新文件夹,其中包含一个history.sqlite文件,但没有导入任何以前的 ipython 命令行历史条目。

任何关于我可以做些什么来让我以前的历史重新启动并运行或如何将文本历史导入history.sqlite数据库的建议都会非常有帮助。

4

1 回答 1

2

遍历旧历史文件的行并通过HistoryManager对象向新历史写入新会话是相当简单的:

from IPython.core.interactiveshell import InteractiveShell
from IPython.core.history import HistoryManager

def migrate_history(old_histfile, new_histfile):
    """populate an IPython sqlite history from an old readline history file"""
    # shell = InteractiveShell.instance()
    history = HistoryManager(hist_file=new_histfile,
        # this line shouldn't be necessary, but it is in 0.13
        shell = InteractiveShell.instance()
    )

    with open(old_histfile) as f:
        # iterate through readline history,
        # and write to the new history database
        for linenum, line in enumerate(f):
            history.store_inputs(linenum, line)

此函数作为脚本,您可以运行一次以将旧的 IPython 历史记录到新的历史记录中。

于 2013-07-15T20:58:33.527 回答