如何为 Windows 在 Git Bash 中启用 Python 上次命令历史记录?

How do I enable Python last command history in Git Bash for Windows?

在 Python 解释器中输入和执行的最后一个 Python 命令可以通过按向上箭头来调用。

这在 Windows CMD 和 Powershell 中按预期工作,但在 Git Bash 中 Windows 向上箭头无效.

或者,如果我尝试 CTRL + P 我得到:

user@DESKTOP MINGW64 ~
$ python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> ^P^P

类似地,Alt + P 正如另一个答案中所建议的那样,只会在命令输入上产生 p

我们如何为 Git Bash 启用 Python 命令历史记录?

在您的 .pythonstartup 文件中添加此行,如果不存在则创建它:

import readline

务必添加 .pythonstartup 文件的完整路径作为名为 PYTHONSTARTUP

的环境变量

这应该可以解决问题。如果出现导入​​错误,请像这样安装它:

pip install pyreadline

或者,如果您使用的是 Anaconda,请使用:

conda install pyreadline