l0b0 l0b0 - 1 year ago 73
Python Question

How to log IPython history to text file?

After some searching and trawling through the IPython documentation and some code, I can't seem to figure out whether it's possible to store the command history (not the output log) to a text file rather than an SQLite database.

ipython --help-all
seems to indicate that this option doesn't exist.

This would be very nice for version controlling frequently used commands like in .bash_history.

Edit: Working solution based on @minrk's answer.

Answer Source

You can emulate bash's behavior by adding this in one of your startup scripts (e.g. $(ipython locate profile)/startup/log_history.py:

import atexit
import os

ip = get_ipython()
LIMIT = 1000 # limit the size of the history

def save_history():
    """save the IPython history to a plaintext file"""
    histfile = os.path.join(ip.profile_dir.location, "history.txt")
    print("Saving plaintext history to %s" % histfile)
    lines = []
    # get previous lines
    # this is only necessary because we truncate the history,
    # otherwise we chould just open with mode='a'
    if os.path.exists(histfile):
        with open(histfile, 'r') as f:
            lines = f.readlines()

    # add any new lines from this session
    lines.extend(record[2] + '\n' for record in ip.history_manager.get_range())

    with open(histfile, 'w') as f:
        # limit to LIMIT entries

# do the save at exit

Note that this emulates the bash/readline history behavior in that it will fail on an interpreter crash, etc.

in a gist

update: alternative

If what you actually want is to just have a few manual favorite commands available to readline (completion, ^R search, etc.) that you can version control, this startup file will allow you to maintain that file yourself, which will be purely in addition to the actual command history of IPython:

import os

ip = get_ipython()

favfile = "readline_favorites"

def load_readline_favorites():
    """load profile_dir/readline_favorites into the readline history"""
    path = os.path.join(ip.profile_dir.location, favfile)
    if not os.path.exists(path):

    with open(path) as f:
        for line in f:

if ip.has_readline:

Drop this in your profile_default/startup/ dir, and edit profile_default/readline_favorites, or anywhere you prefer to keep that file, and it will show up in readline completions, etc. on every IPython session.