Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git 2.5.1's bash console doesn't open python interpreter

Tags:

git

python

bash

People also ask

How do I open Python interpreter in git bash?

To solve this, simply type in winpty python in Git Bash or you can also type python -i Both of these commands will activate the Python shell. One thing to note is this; If you use winpty python you can exit the IDLE using CTRL + Z or exit() .

Why git bash is not opening?

Fixed this by: Open CMD as Admin. CD to the installation path of GitHub (e.g. CMDPRMPT> CD "C:\Program Files\Git") Run "git-bash.exe" from command prompt.

How do I find my python path in git bash?

Solution: At the command prompt, paste this command export PATH="$PATH:/c/Python36" . That will tell Windows where to find Python.

Does git bash have Python?

Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7. 10 like its does in Powershell.


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:

  • It's installed with newer installations of Python (Python 3.3+)
  • It understands and attempts to use the specified installation of python in shebang lines
  • It works with Virtual Environments (shebang line example for venv)

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


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


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.

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


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.)