问题:Python在git bash的命令行中不起作用

Python无法在git bash(Windows)中运行。当我在命令行中键入python时,它带我进入空白行,而无需说它像在Powershell中一样已经输入了python 2.7.10。它不会给我错误消息,但是python不会运行。

我已经确定PATH中的环境变量包括在内c:\python27。我还能检查什么?


发生此问题的会话如下所示:

user@hostname MINGW64 ~
$ type python
python is /c/Python27/python

user@hostname MINGW64 ~
$ 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. It doesn’t give me an error message, but python just doesn’t run.

I have already made sure the environmental variables in PATH included c:\python27. What else can I check?


A session wherein this issue occurs looks like the following:

user@hostname MINGW64 ~
$ type python
python is /c/Python27/python

user@hostname MINGW64 ~
$ python

…sitting there without returning to the prompt.


回答 0

只需在Windows的git shell中输入- alias python='winpty python.exe',就可以了,您将拥有python可执行文件的别名。请享用

PS有关永久别名的添加,请参见下文,

cd ~
touch .bashrc

然后打开.bashrc,从上方添加命令并保存文件。您需要通过控制台创建文件,否则无法使用适当的名称保存文件。您还需要重新启动外壳以应用更改。

Just enter this in your git shell on windows – > alias python='winpty python.exe', that is all and you are going to have alias to the python executable. Enjoy

P.S. For permanent alias addition see below,

cd ~
touch .bashrc

then open .bashrc, add your command from above and save the file. You need to create the file through the console or you cannot save it with the proper name. You also need to restart the shell to apply the change.


回答 1

我在答案列表中没有看到下一个选项,但可以通过“ -i”键获得交互式提示:

$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>> 

I don’t see next option in a list of answers, but I can get interactive prompt with “-i” key:

$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>> 

回答 2

这是MSys2中的一个已知错误,该错误提供了Git Bash使用的终端。您可以通过运行不支持ncurses的Python构建或使用WinPTY解决该问题,方法如下:

要在mintty或Cygwin sshd中运行Windows控制台程序,请在命令行之前添加console.exe:

$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()

用于msys预构建二进制文件可能与Git Bash一起使用。(如果发布此答案以来已经过了很长时间,请检查是否有较新的版本!)。


从Windows 2.7.1的Git开始,也尝试使用winpty c:Python27/python.exe;。WinPTY可能是现成的。

This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:

To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:

$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()

The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there’s a newer version if significant time has passed since this answer was posted!).


As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.


回答 3

我是Windows 10用户,我只接受默认值就已在系统中安装了GIT

阅读以上答案后,我得到了2个解决方案,并且这2个解决方案可完美地在GIT bash上运行,并帮助我在GIT bash 上执行Python语句。

我要附加3张我的GIT图像 bash终端的。第一个有问题,第二个有解决方案。

问题 -光标在按下python命令后才等待

在此处输入图片说明

解决方案1

winpty <path-to-python-installation-dir>/python.exeGIT bash终端上执行。

注意:请勿C:\Users\AdminGIT bash中使用类似路径样式,而应使用/C/Users/Admin

就我而言,我winpty /C/Users/SJV/Anaconda2/python.exeGIT bash 上执行了命令

或者,如果您不知道用户名,请执行winpty /C/Users/$USERNAME/Anaconda2/python.exe

在此处输入图片说明

解决方案2

只需输入即可python -i

在此处输入图片说明

谢谢。

I am windows 10 user and I have installed GIT in my system by just accepting the defaults.

After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.

I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.

PROBLEM – Cursor is just waiting after hitting python command

enter image description here

SOLUTION 1

Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.

Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.

In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash

Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe

enter image description here

SOLUTION 2

Just type python -i and that is it.

enter image description here

Thanks.


回答 4

尝试python -i代替python,这是一个游标。

Try python -i instead of python, it’s a cursor thing.


回答 5

除了@ Charles-Duffy的答案外,您还可以直接使用winpty,而无需安装/下载任何其他内容。赶紧跑winpty c:/Python27/python.exe。可以在Git \ usr \ bin中找到实用程序winpty.exe。我正在为Windows v2.7.1使用Git

@ Charles-Duffy的预编译二进制文件的版本为0.1.1(根据文件名),而随附的二进制文件的版本为0.2.2。

In addition to the answer of @Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I’m using Git for Windows v2.7.1

The prebuilt binaries from @Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2


回答 6

Git Bash解决方法-使用别名启动Python 2和Python 3

。(对我来说)这是在Win 10上直接从Git Bash直接运行Python(Python 2.7和Python 3.x)的最佳解决方案=>将别名添加到Git Bash用于的别名文件中。

Git Bash别名文件aliass.sh它位于:

C:\path where you installed Git\etc\profile.d\aliases.sh

1)(使用Atom等文字编辑器打开)aliases.sh

例如:在我的情况下,文件位于 C:\Software\Develop\Git\etc\profile.d\aliases.sh

2)为Python添加别名

就我而言python.exe安装在:

C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe

所以,你必须创建2别名,一个为Python 2我命名python2)和其他的Python 3我命名只是Python)的Git Bash使用Linux的文件结构,就需要改变“\”“/” ,如果你路径与我的示例网络自动化类似,您可以使用“”

“网络自动化”例如。

winpty是将调用可执行文件的魔术命令。

因此,在aliases.sh的开头添加这些行

alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'

3)添加或修改其他别名(如果需要)

我还修改了ll别名,以显示所有文件并在人类可读的列表中:

alias ll='ls -lah'

4)保存aliases.sh文件


5)好!关闭并重新启动您的Git Bash

现在,您可以永久地从Git shell直接启动两个Python,只需编写

$ python ->启动Python 3

$ python2 ->启动Python 2

$ ll ->输入ls -lah快速显示您的详细文件列表

干杯,哈利

Git Bash Workaround- Launch Python 2 & Python 3 with aliases

HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.

Git Bash aliases file is aliases.sh. It is located in:

C:\path where you installed Git\etc\profile.d\aliases.sh

1) Open (with a text editor like Atom or other) the aliases.sh

for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh

2) Add your alias for Python

In my case the python.exe are installed in:

C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe

So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python) Git Bash uses linux file structure so you need to change the “\” for “/” and if you have a path like my example Network Automation you put it with ” “

“Network Automation”, for ex.

winpty is the magic command that will call the executable.

So add these lines at the beginning of aliases.sh

alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'

3) Add or Modify other aliases (if you want)

I modified also the ll alias to show all the files and in a human readable list:

alias ll='ls -lah'

4) Save the aliases.sh file


5) OK!!! close and relaunch your Git Bash

Now, permanently you could launch both Python directly from Git shell just writting

$ python -> launch Python 3

$ python2 -> launch Python 2

$ ll -> enters a ls -lah to quickly show your detailed file list

Cheers, Harry


回答 7

您可以从以下位置更改Git Bash快捷方式的目标:

"C:\Program Files\Git\git-bash.exe" --cd-to-home 

"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i

这是ConEmu用于启动git bash(版本16)的方式。最新版本正常启动它,这就是我到达那里的方式…

You can change target for Git Bash shortcut from:

"C:\Program Files\Git\git-bash.exe" --cd-to-home 

to

"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i

This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it’s how I got there…


回答 8

类型:“ winpty python”,它将起作用

gitbash在运行以python开头的任何命令时都会遇到一些问题。这也适用于任何python manage.py命令。始终以“ winpty python manage.py”开头至少这对我有用。运行Windows 10。

type: ‘winpty python’ and it will work

gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with ‘winpty python manage.py’ At least this is what works for me. Running Windows 10.


回答 9

除了@Vitaliy Terziev答案

请尝试touch .bash_profile,然后将别名添加到文件中。

In addition to @Vitaliy Terziev answer

try touch .bash_profile and then add alias into the file.


回答 10

2个解决方法,而不是解决方案:在我的Git Bash中,以下命令挂起,并且我没有得到提示:

% python

所以我只用:

% winpty python

正如上面的某些人所指出的,您还可以使用:

% python -i

2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don’t get the prompt back:

% python

So I just use:

% winpty python

As some people have noted above, you can also use:

% python -i

2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line: enter image description here

See attached session.enter image description here


回答 11

我正在Windows 10上通过Visual Studio Code使用MINGW64,并尝试安装node-sass(需要python2)。我在Github上跟随了felixrieseberg / windows-build-tools#56,它解决了我的问题。

这是一个特例,但如果有人遇到相同的问题,我将在此发布:

npm --add-python-to-path='true' --debug install --global windows-build-tools

这会将python和其他必需的构建工具安装到%USERPROFILE%\.windows-build-tools\python27

I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.

This is a special case, but I’m posting in case someone has the same problem:

npm --add-python-to-path='true' --debug install --global windows-build-tools

This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.


回答 12

对于以gitbash作为默认终端的vscode中的python版本3.7.3,我处理了一段时间,然后遵循@Vitaliy Terziev建议将别名添加到.bashrc中,但具有以下规范:

别名python =“’/ c / Users /我的用户名/AppData/Local/Programs/Python/Python37/python.exe”’

注意单引号和双引号的组合,因为“我的用户名”空格。

对我来说,“ winpty”无法解析vscode中的python路径。

For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed @Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:

alias python=’“/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exe”’

Notice the combination of single and double quotes because of “my user name” spaces.

For me, “winpty” couldn’t resolve python path in vscode.


回答 13

此问题的另一个示例是在Windows中使用git bash(MINGW64,Mintty)中的AWS Elastic Beanstalk命令行界面(awsebcli,eb cli)(使用git版本2.19.0.windows.1)。

我之所以发布此帖子,是因为我花了一段时间才能找到此处的eb-cli具体问题。

需要用户输入的命令(例如eb init或)似乎导致冻结/挂起。实际上,我想控制台未使用请求用户输入的文本进行更新。此外,eb config saveeb deploy仅在命令完成后才更新控制台文本,因此直到完成后我才能看到进度更新。

如在git for Windows发行说明(针对v2.19.0)和Xun Yang的答案中所述,一种解决方法是运行

winpty eb <command>(而不是eb <command>

正如本git Windows发行版中所建议的那样,替代方法可能是使用Windows本机控制台而不是mintty(在git安装过程中选择)。

Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).

I’m just posting this because it took me a while to end up here, searching for eb-cli specific issues.

Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don’t get to see progress updates until finished.

As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang’s answer, a workaround is to run

winpty eb <command> (instead of just eb <command>)

A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).


回答 14

如前所述,上面为我工作的一个别名是以下别名:(我正在使用anaconda,因此首先找到python路径在哪里,然后将其添加到git bash上的别名中)。1.在anaconda终端上,我运行:where python 2.在git bash上,我运行:alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"' 3.完成。Python是使用别名在git Bash内部定义的。

感谢(Vitaliy Terziev&hygull)的非常有益的回答。

The one worked for me is as mentioned earlier in these great answers above is the alias as follows: (I’m using anaconda, so first find where is the python path, then add it into the alias on git bash). 1. on anaconda terminal I run: where python 2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"' 3. Done. Python is defined inside the git Bash using the alias.

Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.


回答 15

  1. python.exe -i可以,但是在发送“ ^ Z”(CTRL + Z)退出交互模式时遇到了问题。因此,winpty python.exe在Windows的Git Bash中使用似乎更好。

  2. 使用~/bin目录制作环绕/引用文件(如~/bin/python),该文件可在任何地方访问(您可以使用,如使用不同的版本参考~/bin/python37)。
    文件中的代码:

#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"

# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${@}

我只是不喜欢这些“魔术”别名,您总是忘记它的来源,有时在某些情况下会导致问题。

  1. 使用~/bin/python文件和-i参数:
#!/usr/bin/env bash
if [ -z "${@}" ]; then
    # empty args, use interactive mode
    /c/Users/%USERNAME%/.python/Python36/python.exe -i
else
    /c/Users/%USERNAME%/.python/Python36/python.exe ${@}
fi
  1. python.exe -i works but got issues in exiting from the interactive mode by sending “^Z” (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.

  2. Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
    Code inside the file:

#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"

# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${@}

I just don’t like these “magic” aliases which you’re always forgetting where it’s coming from, and sometimes leads to issues in some cases.

  1. Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${@}" ]; then
    # empty args, use interactive mode
    /c/Users/%USERNAME%/.python/Python36/python.exe -i
else
    /c/Users/%USERNAME%/.python/Python36/python.exe ${@}
fi

回答 16

键入命令PY而不是Python。调用解释器(python.org)。

Type the command PY instead of Python. Invoking the Interpreter (python.org).


回答 17

看看这个答案:

Git Bash无法运行我的python文件吗?

Git Bash中的路径应设置为:

PATH=$PATH:/c/Python27/

Have a look at this answer:

Git Bash won’t run my python files?

the path in Git Bash should be set like this:

PATH=$PATH:/c/Python27/

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。