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

Question:

The last Python command entered and executed in the Python interpreter can be recalled by pressing the up arrow.

This works as expected in the Windows CMD and Powershell, but in Git Bash for Windows the up arrow has no effect.

Alternatively, if I try CTRL + P I get:

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

Similarly, Alt + P as suggested in another answer just produces a p on the command input.

How can we enable Python command history for Git Bash?

Asked By: Michael Fulton

||

Answers:

In your .pythonstartup file add this line or create it if it doesn’t exist:

import readline

Be sure to add the full path of the .pythonstartup file as an environment variable named PYTHONSTARTUP

That should fix it. If you get an import error, install it like so:

pip install pyreadline

Or, if you are using Anaconda, use:

conda install pyreadline
Answered By: xilpex

pyreadline is not maintained anymore. Install pyreadline3 instead, and you don’t need to bother with setting environment variables and startup files.

pip install pyreadline3

Source: https://bugs.python.org/issue45870 (last message)

Answered By: Kostas Mouratidis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.