Python 在 git bash 的命令行中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32597209/
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
Python not working in the command line of git bash
提问by Caroline Morris
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.
Python 不会在 git bash (Windows) 中运行。当我在命令行中输入 python 时,它把我带到一个空行,而不是说它像在 Powershell 中一样进入了 python 2.7.10。它没有给我一条错误消息,但 python 只是不运行。
I have already made sure the environmental variables in PATH included c:\python27
. What else can I check?
我已经确保 PATH 中的环境变量包括在内c:\python27
。我还能检查什么?
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.
...坐在那里没有回到提示。
回答by nevernew
Have a look at this answer:
看看这个答案:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
Git Bash 中的路径应该这样设置:
PATH=$PATH:/c/Python27/
回答by Charles Duffy
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:
这是 MSys2 中的一个已知错误,它提供了 Git Bash 使用的终端。您可以通过在没有 ncurses 支持的情况下运行 Python 构建或使用WinPTY来解决它,使用方式如下:
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()
要在 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()
The prebuilt binaries for msysare likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
msys的预构建二进制文件可能适用于 Git Bash。(如果自此答案发布以来已经过了很长时间,请检查是否有更新的版本!)。
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe
; WinPTY may be included out-of-the-box.
从 Git for Windows 2.7.1 开始,也可以尝试使用winpty c:Python27/python.exe
; WinPTY 可能是开箱即用的。
回答by Xun Yang
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
除了@Charles-Duffy 的回答之外,您还可以直接使用 winpty,而无需安装/下载任何额外的东西。就跑winpty c:/Python27/python.exe
。实用程序 winpty.exe 可以在 Git\usr\bin 中找到。我使用 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
来自@Charles-Duffy 的预构建二进制文件版本为 0.1.1(根据文件名),而包含的版本为 0.2.2
回答by Vitaliy Terziev
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
只需在 windows -> 上的 git shell 中输入它alias python='winpty python.exe'
,就可以了,您将拥有 python 可执行文件的别名。享受
P.S. For permanent alias addition see below,
PS 对于永久别名添加,请参见下文,
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.
然后打开 .bashrc,从上面添加你的命令并保存文件。您需要通过控制台创建文件,否则无法使用正确的名称保存它。您还需要重新启动 shell 以应用更改。
回答by vav
I don't see next option in a list of answers, but I can get interactive prompt with "-i" key:
我在答案列表中没有看到下一个选项,但我可以使用“-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.
>>>
回答by henjiFire
In addition to @Vitaliy Terziev answer
除了@Vitaliy Terziev 的回答
try touch .bash_profile
and then add alias into the file.
尝试touch .bash_profile
然后将别名添加到文件中。
回答by Igonato
You can change target for Git Bash shortcut from:
您可以从以下位置更改 Git Bash 快捷方式的目标:
"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...
这是 ConEmu 用来启动 git bash(版本 16)的方式。最近的版本正常启动,这就是我到达那里的方式......
回答by Bryce Regan LeFevre
type: 'winpty python' and it will work
类型:'winpty python',它会工作
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.
gitbash 在运行任何以 python 开头的命令时有一些问题。这也适用于任何 python manage.py 命令。总是从 'winpty python manage.py' 开始至少这对我有用。运行 Windows 10。
回答by hygull
I am windows 10 user and I have installed GITin my system by just accepting the defaults.
我是 Windows 10 用户,我已经通过接受默认值在我的系统中安装了GIT。
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GITbash and facilitates me to execute Python statements on GITbash.
阅读上述答案后,我得到了 2 个自己的解决方案,这 2 个解决方案在GITbash上完美运行,并有助于我在GITbash上执行 Python 语句。
I am attaching 3 images of my GITbash terminal. 1st with problem and the latter 2 as solutions.
我附上了我的GITbash 终端的3 张图片。第一个有问题,后 2 个作为解决方案。
PROBLEM- Cursor is just waiting after hitting python
command
问题- 光标只是在点击python
命令后等待
SOLUTION 1
解决方案 1
Execute winpty <path-to-python-installation-dir>/python.exe
on GITbash terminal.
winpty <path-to-python-installation-dir>/python.exe
在GITbash 终端上执行。
Note:Do not use C:\Users\Admin
like path style in GITbash, instead use /C/Users/Admin
.
注意:不要C:\Users\Admin
在GITbash 中使用like path 样式,而是使用/C/Users/Admin
.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe
command on GITbash
就我而言,我winpty /C/Users/SJV/Anaconda2/python.exe
在GITbash上执行了命令
Or if you do not know your usernamethen execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
或者,如果您不知道您的用户名,则执行winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
解决方案 2
Just type python -i
and that is it.
只需输入即可python -i
。
Thanks.
谢谢。
回答by Blaskovicz
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 #56on Github which solved my issue.
我在 Windows 10 上通过 Visual Studio Code 使用 MINGW64 并尝试安装node-sass
(需要python2
)。我在 Github 上关注了felixrieseberg/windows-build-tools #56,这解决了我的问题。
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
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
.
这会将 python 和其他所需的构建工具安装到%USERPROFILE%\.windows-build-tools\python27
.