Is there any way to configure the CMD module from Python to keep a persistent history even after the interactive shell has been closed?
When I press the up and down keys I would like to access commands that were previously entered into the shell on previous occasions that I ran the python script as well as the ones I have just entered during this session.
If its any help cmd uses set_completer
imported from the readline module
The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes that will later be wrapped in a more sophisticated interface.
Enter the python REPL in your terminal by entering python (if you have different path variables for python and python3, you may want to enter python3 ). Write a bit of code and call history(1) to see that the last command — history(1) — is printed to the console.
The History Log is stored in the . spyder-py3 (Python 3) or spyder (Python 2) directory in your user home folder (by default, C:/Users/username on Windows, /Users/username for macOS, and typically /home/username on GNU/Linux).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With