Not Sharing python command history between python2 and python3

Tue 16 September 2014 by Fred Clift

I'm finally getting around to switching most-of-the-time to python3.

One minor annoyance was that I had to fix my simple .pythonrc.py file that I use to turn on command history for python2.

In python 3, it appears that readline, tab-completion, and command history between sessions are automatically enabled.

I used to have to do this for python2

in my shell (bash in this case...)

export PYTHONSTARTUP=~/.pypythonrc.py

and then make a file .pythonrc.py in my home directory. The contents of that file, were:

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pythonrc.py, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=~/.pypythonrc.py" in bash.


import atexit
import os
import readline
import rlcompleter
import sys

sys.path.append('/usr/local/lib/python2.7/site-packages')
historyPath = os.path.join(os.environ['HOME'], '.pyhistory')

readline.parse_and_bind('tab: complete')

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)

del os, atexit, readline, rlcompleter, save_history, historyPath, sys

This would turn on readline, tab completion, and load any existing command history so that I could repeat commands with either the arrow keys or good-ol' emacs-bindings of cntrl-p/cntrl-n. It also arranges to save my current history buffer to a file at exit so that it can be reloaded the next time I invoke a shell. It also twiddles my sys.path to include stuff I want, useful mostly for back when I didn't use virtualenv so much.

The mac Python3 build from python.org aparently already does all of this history and tab-completion stuff. but the format used to save history has changed sometime between the version of readline included with 2.7 and 3.4. So the history files are not compatible. Python3 by default uses the name .python_history. I'm renaming my old history file to be .pyhistory2 so there is no confusion later.

I now do this:

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pythonrc.py, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=~/.pypythonrc.py" in bash.

import sys

if sys.version_info.major == 2 and sys.version_info.minor == 7:
    import atexit
    import os
    import readline
    import sys
    sys.path.append('/usr/local/lib/python2.7/site-packages')
    historyPath = os.path.join(os.environ['HOME'], '.pyhistory2')
    readline.parse_and_bind('tab: complete')

    def save_history(historyPath=historyPath):
        import readline
        readline.write_history_file(historyPath)

    if os.path.exists(historyPath):
        readline.read_history_file(historyPath)

    atexit.register(save_history)
    del save_history, historyPath, os, atexit, readline

del sys

#other stuff?

While python 3 and 2 don't share history, at least it works in both.