【问题标题】:Recovering / Importing missing IPython history after an upgrade升级后恢复/导入丢失的 IPython 历史记录
【发布时间】:2013-07-13 04:18:43
【问题描述】:
我最近执行了pip install --upgrade ipython,因此将我的旧 ipython 版本更新为 0.13.2。从那时起,IPython 开始使用 sqlite 文件来存储其历史记录。
显然 $HOME/.ipython/history 中的旧历史文件完好无损,但未使用。而是创建了一个新文件夹 profile_default,其中包含 history.sqlite 文件,但没有导入任何以前的 ipython 命令行历史条目。
任何关于我可以做些什么来重新启动并运行我以前的历史记录或如何将文本历史记录导入history.sqlite 数据库的任何建议都会非常有帮助。
【问题讨论】:
标签:
sqlite
upgrade
history
pip
ipython
【解决方案1】:
遍历旧历史文件的行相当简单,
并通过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)
这个函数as a script,您可以运行一次,将旧的 IPython 历史记录到新的历史记录中。