6

Good afternoon,

The Question

Is there a particular spot that the entries are stored, or is it just a local set of stored variables, for the windows version of Python?

The Context

I am curious about where Python.exe stores the entries typed out from the command line, not the Tkinter GUI. I'm particularly interested in this because I've noticed you can resort to previous entries if you are typing in raw_input(). There have been many discussions in regards to raw_input() and input() receiving some sort of default argument that is editable.

See. This link to a related SO question

Obviously this is very doable with Linux / GNU with modules that will allow you to take advantage of Bash's format. Windows users are unlucky in that regard.

Notes on these modules:

readline is GNU based and does not work properly in Windows.

pyreadline does not benefit the command line interpreter.

curses is GNU based and does not work at all in Windows.

A Follow up

The conclusion I'm arriving to is that if the user is given a pre-defined set of entries, you can limit what is entered into the command space.


P.S. - I understand it is much easier to just create a PySide, Tkinter or "other"-based GUIs to get around what I'm asking.

Sir James

SirJames
  • 387
  • 8
  • 27
  • 1
    For cooked reads, the Windows console (conhost.exe) implements command-line editing, input history (e.g. F7 history box), and aliases -- remembered by process image name for each attached process using a separate history buffer. The number of history buffers is set in the console properties. This setting is persisted in the registry for the default settings (`HKCU\Console`) or subkeys named by console title, or instead in the shortcut file (.LNK) that started the console. However, input history itself is not persisted and can't be reloaded from disk. Each conhost.exe instance starts fresh. – Eryk Sun Feb 17 '17 at 19:55
  • 1
    Console input history can be queried using the undocumented (and unsupported) kernel32 functions `GetConsoleCommandHistoryLengthW` and `GetConsoleCommandHistoryW`. Or just run `doskey.exe /history`, which calls these functions for you. – Eryk Sun Feb 17 '17 at 20:01

1 Answers1

5

On Windows 7, using the standard Python 3.7 command interpreter (not IPython or IDLE), the command history is stored in the file %USERPROFILE%\.python_history. This location is not used for Python 2.7.x, as the Python command history feature was introduced starting with Python 3.4.

JPaget
  • 969
  • 10
  • 13
  • 1
    Do you know how exactly the history gets written ? Is that builtin into the interpreter, or is there an extra module that writes the history file ? – TheEagle Apr 30 '21 at 10:34
  • @Programmer It's done by `readline` [here](https://github.com/python/cpython/blob/99fcf1505218464c489d419d4500f126b6d6dc28/Lib/site.py#L473)., like this: `history = os.path.join(os.path.expanduser('~'),'.python_history')`. Unfortunately this is a big mistake, as they have hard-coded the path and thus not accessible for changes within REPL, apart the discussion of using an environment variable. Also their internal and github docs are not updated, and show the wrong info as using `~/.history`. – not2qubit Jan 24 '22 at 01:49
  • I find that this is not the case for Python 3.9 (Anaconda on Windows 10, i.e., there is no history file by any name in folder c:\Users\User.Name, at least when running the command `python` from the conda prompt (which is the use case that I'm currently interested in). – user2153235 Jun 28 '23 at 15:56