5

I'm working on the default python interpreter on Windows 10, and I can go through them one by one using the arrow keys. But is there an option like the history command in bash shell, which shows you all the commands you've entered so far?

This is a near duplicate of How do you see the entire command history in interactive Python?, except that it is for Windows 10 instead of macos/*nix. The solutions over there either recommend iPython, which I'm not often using, or use readline, which is not available on Windows.

I have installed the pyreadline package, but it doesn't seem to be a drop-in replacement in this case.

C:\Users\yoder>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
  • 1
    if you are running python from windows cmd, the accepted answer from linked topic works. – Vova Jul 08 '20 at 20:23
  • You're right! Somehow, I thought that because `pip install readline` fails, that `import readline` would too! – Josiah Yoder Jul 08 '20 at 20:34
  • F7 key pops up a text box in the console that lists all lines in the current history buffer. doskey.exe can also get the history, for which it uses undocumented functions in the console API. – Eryk Sun Jul 08 '20 at 20:35
  • @ErykSun I'm ignorant enough of these things that I don't know. All know is that I type `python` in the prompt, and then I see the output that I just edited into my answer above. – Josiah Yoder Jul 08 '20 at 20:42
  • @ErykSun Also note that my question is already answered by an existing question, so I'm considering deleting this question or marking it as a duplicate. – Josiah Yoder Jul 08 '20 at 20:43
  • 1
    If you have pyreadline installed (i.e. `import readline` works), you won't be using the console's cooked read that stores history the normal way in the console that's accessible via F7. The history with readline enabled is stored in Python's history file. But note that pyreadline is poorly maintained and doesn't properly support (i.e. it's buggy) the way the console implements pasting non-keyboard Unicode characters. – Eryk Sun Jul 08 '20 at 20:43
  • Answers on the other question make no mention of pyreadline, but it's the only way to get readline support in the Windows console, so it can be assumed. – Eryk Sun Jul 08 '20 at 20:46
  • @ErykSun I think that's an answer. Do you want to write it? – Josiah Yoder Jul 08 '20 at 20:50
  • It seem that both `pyreadline` and `readline` write to the history file, **only upon exit from REPL**. (At least on Windows.) Therefore, if you want to a have a continuous *last-command-written* function, you need to write your own, and add it to your `~/.pyrc` using the `PYTHONSTARTUP` file. – not2qubit Jan 15 '22 at 09:28
  • @not2qubit That feels like the start of a new question to me. Feel free to ask & answer it, and link it here. – Josiah Yoder Feb 03 '22 at 13:37

1 Answers1

4

To summarize what ErykSun and Vova said in the comments on the question:

If you are running Python within a cmd prompt and have not installed pyreadline, you can press F7 to bring up cmd's native history, which Python uses for its own history.

If you have installed pyreadline, then you can import readline and use the techniques described in this answer.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58