37

Is there a way to tell the interactive Python shell to preserve its history of executed commands between sessions?

While a session is running, after commands have been executed, I can arrow up and access said commands, I'm just wondering if there is some way for a certain number of these commands to be saved until the next time I use the Python shell.

This would be very useful since I find myself reusing commands in a session, that I used at the end of the last session.

Ry-
  • 209,133
  • 54
  • 439
  • 449
mjgpy3
  • 8,053
  • 4
  • 27
  • 48
  • possible duplicate of [How to save a Python interactive session?](http://stackoverflow.com/questions/947810/how-to-save-a-python-interactive-session) – brandizzi Dec 01 '14 at 13:13

3 Answers3

42

Sure you can, with a small startup script. From Interactive Input Editing and History Substitution in the python tutorial:

# 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 ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=~/.pystartup" in bash.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

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

From Python 3.4 onwards, the interactive interpreter supports autocompletion and history out of the box:

Tab-completion is now enabled by default in the interactive interpreter on systems that support readline. History is also enabled by default, and is written to (and read from) the file ~/.python-history.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
  • Thank you, this is what I was looking for! – mjgpy3 Sep 08 '12 at 21:08
  • I have several python virtual environments and wanted to be able to enable persistent history. So used this approach, but changed the location of the `.pyhistory` file to the virtual environment folder instead of user home folder. – Antony Mar 17 '18 at 19:05
17

Use IPython.

You should, anyway, because it's awesome: persistent command history is just one of the many many ways it's better than the stock Python shell.

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842
1

This is also required for Python 3 when using a virtual environment.

I use a slightly different version which keeps a history file per virtual environment:

import sys

if sys.version_info >= (3, 0) and hasattr(sys, 'real_prefix'):  # in a VirtualEnv
    import atexit, os, readline, sys

    PYTHON_HISTORY_FILE = os.path.join(os.environ['VIRTUAL_ENV'], '.python_history')
    if os.path.exists(PYTHON_HISTORY_FILE):
        readline.read_history_file(PYTHON_HISTORY_FILE)
    atexit.register(readline.write_history_file, PYTHON_HISTORY_FILE)
Javier
  • 2,544
  • 14
  • 27