Create a gist now

Instantly share code, notes, and snippets.

Migrate old IPython history (readline) to new IPython history (sqlite)
Author: Min RK (
License: Public Domain
import os
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.history import HistoryManager
from IPython.utils.path import locate_profile, get_ipython_dir
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)
if __name__ == '__main__':
# the default location of old history file
old = os.path.join(get_ipython_dir(), "history")
# the default location of the new history file
new = os.path.join(locate_profile(), "history.sqlite")
migrate_history(old, new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment