Give the Python Terminal a Persistent History

Give the Python Terminal a Persistent History

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.

Give the Python Terminal a Persistent History

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.

Persistent Python Command-Line History

I think the suggestions in the Python documentation pretty much cover what you want. Look at the example pystartup file toward the end of section 13.3:

  • http://docs.python.org/tutorial/interactive.html

or see this page:

  • http://rc98.net/pystartup

But, for an out of the box interactive shell that provides all this and more, take a look at using IPython:

  • http://ipython.scipy.org/moin/

How do you see the entire command history in interactive Python?

Use readline.get_current_history_length() to get the length, and readline.get_history_item() to view each.

Persistent history in python cmd module

readline automatically keeps a history of everything you enter. All you need to add is hooks to load and store that history.

Use readline.read_history_file(filename) to read a history file. Use readline.write_history_file() to tell readline to persist the history so far. You may want to use readline.set_history_length() to keep this file from growing without bound:

import os.path
try:
import readline
except ImportError:
readline = None

histfile = os.path.expanduser('~/.someconsole_history')
histfile_size = 1000

class SomeConsole(cmd.Cmd):
def preloop(self):
if readline and os.path.exists(histfile):
readline.read_history_file(histfile)

def postloop(self):
if readline:
readline.set_history_length(histfile_size)
readline.write_history_file(histfile)

I used the Cmd.preloop() and Cmd.postloop() hooks to trigger loading and saving to the points where the command loop starts and ends.

If you don't have readline installed, you could simulate this still by adding a precmd() method and record the entered commands yourself.

No persistent command history in Python 3.5 (virtualenv)

As the answer is hidden in the comments and therefore hard to find:

From https://unix.stackexchange.com/a/121390 from @Jason's comment:

Create a .pythonrc.py file:

import os
import atexit
import readline

readline_history_file = os.path.join(
os.path.expanduser('~'),
'.python_history'
)
try:
readline.read_history_file(readline_history_file)
except IOError:
pass

atexit.register(readline.write_history_file, readline_history_file)

and export it by adding the following line to your ~/.bashrc:

export PYTHONSTARTUP=$HOME/.pythonrc.py

How can I access command prompt history with Python

You don't need Python at all, use doskey facilities for that, i.e.:

doskey /history

will print out the current session's command history, you can then redirect that to a file if you want to save it:

doskey /history > saved_commands.txt

If you really want to do it from within Python, you can use subprocess.check_output() to capture the command history and then save it to a file:

import subprocess

cmd_history = subprocess.check_output(["doskey", "/history"])
with open("saved_commands.txt", "wb") as f:
f.write(cmd_history)


Related Topics



Leave a reply



Submit