0

I am following this Q&A's procedure for accessing Python's REPL command history -- specifically at the Conda prompt, not in an IDE.

I am using Python 3.9 installed under Anaconda on Windows 10. Version 3.9 needed for backward compatibility with colleagues' past work.

The history is apparently handled by pyreadline3.Readline, which is the implementation of Gnu's readline for Python 3. I am relying on Anaconda to manage the package depedencies. Unfortunately, the REPL commands aren't being captured.

Annex A shows a session transcript in the Conda environment py39, which contains Python 3.9. It contains three chunks:

  1. Installation of pyreadline3

  2. Test-driving the history capture in Python, which fails

  3. Using Cygwin's Bash to confirm that the written history file is empty.

What else can I do to enable history capture?

Annex A: Session transcript

REM Insteall pyreadline3
REM --------------------
(py39) C:\Users\User.Name> conda install pyreadline3
<...snip...>
Collecting package metadata (current_repodata.json): \ DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443
<...snip...>
done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
  current version: 23.7.2
  latest version: 23.7.3
Please update conda by running
    $ conda update -n base -c defaults conda
Or to minimize the number of packages updated during conda update use
     conda install conda=23.7.3
## Package Plan ##
  environment location: C:\Users\User.Name\anaconda3\envs\py39
  added / updated specs:
    - pyreadline3
The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    pyreadline3-3.4.1          |   py39haa95532_0         148 KB
    ------------------------------------------------------------
The following NEW packages will be INSTALLED:
  pyreadline3        pkgs/main/win-64::pyreadline3-3.4.1-py39haa95532_0
Proceed ([y]/n)? y <RETURN>
Downloading and Extracting Packages
pyreadline3-3.4.1    | 148 KB    |                                                                              |   0% DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): repo.anaconda.com:443
DEBUG:urllib3.connectionpool:https://repo.anaconda.com:443 "GET /pkgs/main/win-64/pyreadline3-3.4.1-py39haa95532_0.conda HTTP/1.1" 200 151256
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

REM Illustrate in Python that commands aren't capture
REM -------------------------------------------------
(py39) C:\Users\User.Name> python
Python 3.9.17 (main, Jul  5 2023, 21:22:06) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyreadline3 import Readline
>>> readline = Readline()
>>> readline.get_current_history_length() # Commands captured
0
>>> readline.get_history_length() # Length of buffer
100
>>> import os # Write history to file
>>> os.getcwd()
   'C:\\Users\\User.Name'
>>> readline.write_history_file("./pyHistory.txt")

# Use Cygwin's Bash to show that target history file is empty
#------------------------------------------------------------
User.Name@Laptop-Name /c/Users/User.Name
$ new #  This is "ls -ltA", cleaned up with "sed"
              0 2023-08-30 13:10 pyHistory.txt*
        2359296 2023-08-29 01:39 NTUSER.DAT*
        <...snip...>
user2153235
  • 388
  • 1
  • 11

0 Answers0