Commands not stored in command history

156 Views Asked by At

Background: This answer relies on the readline module and write_history_file function therein. I have to account for differences using the Conda prompt on Windows 10 (which is just the CMD prompt with environment variables set for Conda). For this there is no history file in c:\User.Name. Additionally, I need pyreadline3.

No commands are stored in the command history even though there is room for 100 commands. Commands to launch Python, import pyreadline3, query the command history size, and write the empty command history to a file:

REM   "CMD" commands in Windows to start Python 3.9
REM   environment from Conda prompt
conda activate py39
python

# Load and alias derivative of readline module for Windows.
# From https://stackoverflow.com/a/76566474/2153235
import pyreadline3
readline = pyreadline3.BaseReadline()

# Maximum number of commands storable is 100
readline.get_history_length()

# Sacrifical commands to test the command history
"dog"
'cat'
help(readline)
   # Shows get_history_length and get_current_history_length

# Number of stored commands is persistently 0
readline.get_current_history_length()

# Therefore, a zero-length history file is written.
# From https://stackoverflow.com/a/76566474/2153235
readline.write_history_file("./pyHistory.txt")

# Loop prints out zero historical commands.
# From https://stackoverflow.com/questions/6558765, with "list"
# variations at https://stackoverflow.com/questions/44894992
for i in range(readline.get_current_history_length()):
    print (readline.get_history_item(i + 1))

How to get this working?

F7 can bring up a context menu of past commands, but that's far from being able to browse a large history and doesn't allow mashing/bashing to recompose commands. Also doesn't work when I use Cygwin's xterm to invoke the CMD command that launches Conda. I've done this to capture input and output of a session using TypeScript. It's easier to navigate and search the recorded text file than scrolling up the terminal window. Typical terminals have a limited buffer size.

Someone suggested with Jupyter it isn't necessary to have a history file. It seems to serve a different purpose than a history file and doesn't record commands executed so it can be difficult to reconstruct what events affected state of objects at various scopes in the stack frame and the stack frame itself. One can look at the terminal but buffer size is limited and the voluminous output can make it hard to reconstruct exact input. I also found that %USERPROFILE%\.python_history captures REPL commands but does not update until one exits the Python REPL session.

0

There are 0 best solutions below