首页 > 解决方案 > 如何在 Windows 版 Git Bash 中启用 Python 上次命令历史记录?

问题描述

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

这在 Windows CMD 和 Powershell 中按预期工作,但在 Git Bash for 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+只会在命令输入上产生 a 。Pp

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

标签: pythongit-bash

解决方案


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

import readline

请务必将.pythonstartup文件的完整路径添加为名为的环境变量PYTHONSTARTUP

那应该解决它。如果遇到导入错误,请按如下方式安装:

pip install pyreadline

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

conda install pyreadline

推荐阅读