Git 2.5.1 的 bash 控制台不打开 python 解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32454589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:32:24  来源:igfitidea点击:

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

pythongitbash

提问by Manu

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.

如果我在 CMD 中执行它,它可以正常工作,但是如果我在 Git Bash 中尝试它,则它不起作用。我喜欢使用 Git Bash 作为我唯一的控制台,但如果它不适用于 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.

示例如下图所示。这可以很容易地重现。如果安装了 Python 和 Git,请卸载它们,安装 Python 3.4,安装 Git 2.5.1,然后你就会得到那个结果。

Console

安慰

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

如何让 Python 解释器在 Git Bash 中工作?

回答by Casey Kuball

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:

作为 Git 新默认终端的 MinTTY 终端根本不支持 Windows 控制台程序。我不知道为什么决定更改默认终端,但我知道有几种方法可以解决这个问题:

  1. Write a Bash alias to launch python with winpty
  1. 编写 Bash 别名以使用 winpty 启动 python

Bash Alias (put in your .bashrc):

Bash 别名(放入您的 .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.

注意:从 Git for Windows 2.7.1 开始,Winpty 是开箱即用的。winpty 可以安装在Git\usr\bin.



  1. Write a Bash alias to launch python in interactive mode if there are no arguments:
  1. 如果没有参数,则编写一个 Bash 别名以在交互模式下启动 python:

Bash Alias (put in your .bashrc):

Bash 别名(放入您的 .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
  1. 以交互模式显式启动 python

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?

py.exe 是什么?

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

如果您想知道为什么我要引用C:\Windows\py.exe而不是特定的python.exe安装,我想解释一下使用它的一些好处(WindowsPython 启动器

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

要更改您的首选/系统安装(例如,对于交互模式),请参阅此答案

回答by besil

You need to explicit python interactive mode: python -i

你需要显式python交互模式: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).

您可以在 .bashrc 中定义别名:alias python='python -i',但是这样做,您将无法运行脚本文件(即:python script.py)。

Found here: Using Windows Python from Cygwin

在这里找到: 使用来自 Cygwin 的 Windows Python

回答by Dr Manhattan

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

以@Darthfett 的回答为基础。我必须确保有引号而不是引用 .exe 文件

So in the end in your .bashrc

所以最后在你 .bashrc

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

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

Then is all works

然后就是所有的工作

Python

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.

回答by andygavin

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:

它正在尝试打开控制台以进行输出。除非您编译 python 或获取 mingw 的版本,否则您可能需要以下内容:

WinPty

WinPty

回答by RayLuo

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

感谢@darthfett 的回答,这在很大程度上解决了问题!

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

仅供参考:当您的脚本使用时也存在相同的症状import getpass; getpass.getpass(),在这种情况下python -i your_script.py不会修复它,但winpty python your_script.py仍然像魅力一样工作。(幸运的是,他们至少为 Winpty 提供了最新版本的 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:

因此,要设置一次(每个虚拟环境)并忘记它,您可以在您的末尾附加此行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.pyin vim.)

它将在那个 bash 控制台中工作。(但是,如果您碰巧是 vim 用户,当您:python my_script.py在 vim 中使用时,它仍然无法在 vim 中工作。)

回答by Keziah Kitone

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.

安装 git for windows 时,选择使用 windows 默认控制台窗口,如下图所示。此选项允许您使用交互式 python 或 nodejs。getpass 也适用于这个控制台。

enter image description here

在此处输入图片说明

回答by Keziah Kitone

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.

当安装Windows混帐,选择使用Windows默认的控制台窗口,如图所示画面。此选项允许您使用交互式 python 或 nodejs。getpass 也适用于这个控制台。

回答by edi9999

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

您可以通过编辑“$HOME/.bashrc”中的文件来配置 git bash 控制台

Add this line to your $HOME/.bashrc

将此行添加到您的 $HOME/.bashrc

export PATH=$PATH;c:/python34