Git 2.5.1的bash控制台没有打开python interp

2024-04-20 04:42:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我在CMD中这样做,它可以正常工作,但如果我在Git Bash中尝试,它就不起作用。我喜欢使用Git Bash作为我唯一的控制台,但是如果它不能与Python 3.4一起工作,我就不能这样做。

示例如下图所示。这很容易复制。卸载Python和Git如果安装了它们,安装Python 3.4,安装Git 2.5.1,就会得到这个结果。

Console

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


Tags: gitcmdbash示例解释器
3条回答

您需要显式python交互模式:python-i

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

在此处找到: Using Windows Python from Cygwin

MinTTY终端是Git的新默认终端,它根本不支持Windows控制台程序。我不知道为什么决定更改默认终端,但我知道一些解决方法:

  1. 编写Bash别名以使用winpty启动python

Bash别名(输入您的.bashrc):

alias python=winpty py.exe

注意:从Git for Windows 2.7.1开始,Winpty即已包含。可以在Git\usr\bin找到winpty。


  1. 如果没有参数,请编写Bash别名以交互模式启动python:

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. 以交互模式显式启动python

请注意,使用箭头键浏览命令历史可能无法正常工作:

py -i

或者对于脚本:

py script.py

什么是py.exe?

如果你想知道我为什么引用C:\Windows\py.exe而不是特定的python.exe安装,我想解释一下使用它的一些好处(thePython Launcher for Windows

有关更改首选/系统安装(例如,对于交互模式),请参见this answer

根据达特菲特的回答。我必须确保有引号,而不是引用.exe文件

所以最后在你的.bashrc

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

那一切都好了吗

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.

相关问题 更多 >