Background (from here, though the question differs): I am following this answer on writing the Python command history to a file, which relies on the readline module and the write_history_file function therein. I have to account for differences in using the Conda prompt on Windows 10, which is just the CMD prompt with environment variables set for Conda. For this use case there is no history file in c:\User.Name, which is typically the case. Additionally, I need pyreadline3.
My problem is that no commands seem to be stored in the command history, even though there is room for 100 commands. Here are the exact commands to launch Python, import pyreadline3, query the command history size, and write the empty command history to a file. Comments provide the citations to the source Q&A's on Stack Overflow:
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))
What else can I do to track down the problem and/or get this working?
For me, the simpler the better. I am trying to access a live command history because it helps me experiment and get started with Python (and Conda), coming from a Matlab where the history is always available.
I realize that F7 can bring up a context menu of past commands, but that's a far cry from being able to browse a large history using (say) Vim or Emacs, and it doesn't allow for text mashing/bashing to recompose series of commands from the history.
F7 also doesn't work when I use Cygwin's xterm to invoke the CMD command that launches Conda -- I've done this just to capture the input and output of a session using typescript. It's a bit easier to navigate and search the recorded text file using legacy editors rather than scrolling up the terminal window. Typical terminals have a limited buffer size for both input commands and output combined, with the lion's share take up by output.
Someone suggested to me that with Jupyter, it isn't necessary to have a history file. I have yet to get experience with Jupyter beyond a simple scouting trial, but it seems to serve a different purpose than a history file. It doesn't actually record a history of commands that were executed, so it can be difficult to reconstruct what events may have affected the state of the Python "process" (that's the only word that I can think of for the state of the objects at various scopes in the stack frame and the stack frame itself).
One can of course look at the text content on the terminal for this, but as indicated above, the buffer size is limited. Furthermore, the voluminous output can make it hard to reconstruct that exact input that drove the Python process. I believe that this is at least one major reason why history files are provided in many coding environments (or histories via IDE subwindows).