33

If I do it in CMD, it works without issues, but if I try it in Git Bash it doesn't work. I like to use Git Bash as my only console, but I can't do that if it doesn't work with Python 3.4.

Example is in the picture below. This can be easily reproduced. Uninstall Python and Git if they are installed, install Python 3.4, install Git 2.5.1 and you get that result.

Console

How can I make the Python interpreter work in Git Bash ?

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Law
  • 483
  • 1
  • 5
  • 9
  • I'm having the same problem. Have you found a workaround? Running python -c "" works. The problem seems related only to the interactive shell – besil Oct 20 '15 at 10:01

8 Answers8

46

The MinTTY terminal that is the new default terminal for Git simply doesn't support Windows console programs. I don't know why the decision was made to change the default terminal, but I know a few ways to work around this:

  1. Write a Bash alias to launch python with winpty

Bash Alias (put in your .bashrc):

alias python=winpty py.exe

Note: As of Git for Windows 2.7.1, Winpty is included out of the box. winpty can be found installed at Git\usr\bin.


  1. Write a Bash alias to launch python in interactive mode if there are no arguments:

Bash Alias (put in your .bashrc):

function maybe_py() {
    if [ $# -eq 0 ]; then
        /c/Windows/py.exe -i
    else
       /c/Windows/py.exe $@
    fi
}

alias python=maybe_py

  1. Launch python in interactive mode explicitly

Note that this may not work correctly using arrow keys to browse command history:

py -i

Or for scripts:

py script.py

What Is py.exe?

In case you are wondering why I'm referencing C:\Windows\py.exe instead of a particular python.exe installation, I wanted to explain a few benefits of using it (the Python Launcher for Windows:

For changing your preferred/system installation (e.g. for interactive mode), see this answer.

Casey Kuball
  • 7,717
  • 5
  • 38
  • 70
  • 3
    Comment to myself: `python -i` with the new git bash console doesn't always work correctly if you are trying to use the arrow keys to retrieve command history. It seems to move the cursor up through the output, as if it was a text editor. – Casey Kuball Nov 16 '15 at 15:59
  • 2
    @Sushil I just researched and saw an update [here](http://stackoverflow.com/questions/32597209/python-not-working-in-the-command-line-of-git-bash). It seems the MinTTY terminal will not support windows console programs out of the box, but if you install Git For Windows 2.7.1 or higher, you can use Winpty out of the box to run python. I have updated the answer appropriately. – Casey Kuball Jul 20 '16 at 18:23
  • 1
    FYI: Same symptom exists when your script is using `getpass.getpass()`, and `python -i your_script.py` will NOT fix it, but `winpty python your_script.py` works like a charm. Lucky that they at least provide Winpty out of box with recent versions of Git For Windows. – RayLuo Mar 06 '17 at 23:41
  • I think you need quotes around the alias since it contains a space. – JoshOrndorff Dec 20 '17 at 20:28
  • Works for me: `alias python="winpty python.exe"` – mkczyk Apr 09 '19 at 21:39
15

You need to explicit python interactive mode: python -i

You can define an alias in your .bashrc: alias python='python -i', but doing this, you will not be able to run a script file (i.e.: python script.py).

Found here: Using Windows Python from Cygwin

Community
  • 1
  • 1
besil
  • 1,308
  • 1
  • 18
  • 29
  • 3
    `python -i` with the new git bash console doesn't always work correctly if you are trying to use the arrow keys to retrieve command history. It seems to move the cursor up through the output, as if it was a text editor. – Casey Kuball Nov 16 '15 at 15:59
  • Yes, this is true. It's a pity, but this is the only way I found the interpreter working. – besil Nov 16 '15 at 16:06
  • If you run `winpty python` then you get the python console with the arrow keys working and `^Z + Return` exits the console – Joman68 Sep 05 '19 at 22:53
5

Building onto @Darthfett's answer. I had to make sure there were quote marks and not reference the .exe files

So in the end in your .bashrc

alias python='winpty python' alias pip='winpty pip' # Rescue pip as well

Then is all works

Python

Tawanda@Tawanda-PC MINGW64 ~
$ alias python='winpty python'

Tawanda@Tawanda-PC MINGW64 ~
$ python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Pip

Tawanda@Tawanda-PC MINGW64 ~
$ alias pip='winpty pip'

Tawanda@Tawanda-PC MINGW64 ~
$ pip -v

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41
3

It's trying to open the console for the output. Unless you compile python or get a version for mingw you may need something like:

WinPty

andygavin
  • 2,784
  • 22
  • 32
2

Thanks for @darthfett 's answer, which largely solves the problem!

Just FYI: Same symptom also exists when your script is using import getpass; getpass.getpass(), and in this case python -i your_script.py will NOT fix it, but winpty python your_script.py still works like a charm. (Lucky that they at least provide Winpty out of box with recent versions of Git For Windows.)

So, to setup once (per virtual environment) and forget it, you can append this line at the end of your env/Script/activate:

alias python='winpty python.exe'

It will work in that bash console. (However, if you happen to be a vim user, it still won't work inside a vim when you do :python my_script.py in vim.)

Community
  • 1
  • 1
RayLuo
  • 17,257
  • 6
  • 88
  • 73
0

You can configure the git bash console by editing the file in your "$HOME/.bashrc"

Add this line to your $HOME/.bashrc

export PATH=$PATH;c:/python34
edi9999
  • 19,701
  • 13
  • 88
  • 127
  • 1
    `$HOME/.bashrc` does not exist by default. Creating it and adding that line, then restarting Git Bash does not make it work with Python. – Law Sep 08 '15 at 10:17
0

When installing git for windows, choose to use windows default console window as shown in the picture below. This option allows you to use interactive python or nodejs. Also getpass works on this console.

enter image description here

0

When installing git for windows, choose to use windows default console window as shown in the picture. This option allows you to use interactive python or nodejs. Also getpass works on this console.