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

后端 未结 8 1685
小鲜肉
小鲜肉 2021-01-31 08:56

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 wit

相关标签:
8条回答
  • 2021-01-31 09:28

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

    0 讨论(0)
  • 2021-01-31 09:32

    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 讨论(0)
  • 2021-01-31 09:37

    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.

    0 讨论(0)
  • 2021-01-31 09:37

    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

    0 讨论(0)
  • 2021-01-31 09:37

    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.

    0 讨论(0)
  • 2021-01-31 09:39

    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
    
    0 讨论(0)
提交回复
热议问题