Lorenz Lo Sauer
Lorenz Lo Sauer

Reputation: 24740

Recovering / Importing missing IPython history after an upgrade

I performed a pip install --upgrade ipython recently, thus updating my old ipython version to 0.13.2. Since then IPython started using an sqlite file for storing its history.

Apparently the old history file in $HOME/.ipython/history is intact, but not used. Instead a new folder profile_default was created with a history.sqlite file in it, but without importing any previous ipython commandline-history entries.

Any advice on what I could do to get my previous history up and running again or how to import the text-history into the history.sqlite database, would be very helpful.

Upvotes: 0

Views: 394

Answers (1)

minrk
minrk

Reputation: 38608

It's fairly straighforward to iterate over the lines of the old history file, and write a new session to the new history via the HistoryManager object:

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)

This function as a script, which you can run once to get your old IPython history into your new one.

Upvotes: 2

Related Questions