Git Bash 不会运行我的 python 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22869192/
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-08-19 01:51:00  来源:igfitidea点击:

Git Bash won't run my python files?

pythonwindowsgitbash

提问by user3496571

I have been trying to run my python files in Git Bash but I keep getting an error and can't figure out how to fix it. My command as follows in the git bash executable python filename.pythen it says

我一直在尝试在 Git Bash 中运行我的 python 文件,但我不断收到错误,无法弄清楚如何修复它。我的命令在 git bash 可执行文件中如下所示,python filename.py然后它说

"Bash.exe": python.exe: command not found

I'm a windows user and I have added the path to my environment variables like so C:\Python27\python.exe;C:\Program Files\Git\bin\bash.exe

我是 Windows 用户,我已经将路径添加到我的环境变量中 C:\Python27\python.exe;C:\Program Files\Git\bin\bash.exe

I have been looking around but I can't find anyone that has had this problem or they don't give a straightforward answer please help.

我一直在环顾四周,但找不到任何遇到此问题的人,或者他们没有给出直接的答案,请帮助。

Also I have never used Git before this is my first time.

此外,在这是我第一次之前,我从未使用过 Git。

采纳答案by gturri

Adapting the PATHshould work. Just tried on my Git bash:

适应PATH应该工作。刚刚试过我的 Git bash:

$ python --version
sh.exe": python: command not found

$ PATH=$PATH:/c/Python27/

$ python --version
Python 2.7.6

In particular, only provide the directory; don't specify the .exeon the PATH; and use slashes.

特别是,只提供目录;不要.exePATH;上指定 并使用斜线。

回答by Zacarias Bendeck

That command did not work for me, I used:

该命令对我不起作用,我使用了:

$ export PATH="$PATH:/c/Python27"

Then to make sure that git remembers the python path every time you open git type the following.

然后确保每次打开 git 时 git 都记住 python 路径,请键入以下内容。

echo 'export PATH="$PATH:/c/Python27"' > .profile

回答by Jasper Kinoti

This works great on win7

这在win7上效果很好

$ PATH=$PATH:/c/Python27/ $ python -V Python 2.7.12

$ PATH=$PATH:/c/Python27/ $ python -V Python 2.7.12

Screenshot

截屏

回答by J4cK

Add following line in you .bashrc file

在您中添加以下行 .bashrc file

############################
# Environment path setting #
############################
export PATH=/c/Python27:/c/Python27/Scripts:$PATH

回答by H?kan Nilsson

When you install python for windows, there is an option to include it in the path. For python 2 this is not the default. It adds the python installation folder and script folder to the Windows path. When starting the GIT Bash command prompt, it have included it in the linux PATH variable.

当你为 windows 安装 python 时,有一个选项可以将它包含在路径中。对于 python 2,这不是默认值。它将python安装文件夹和脚本文件夹添加到Windows路径。启动 GIT Bash 命令提示符时,它已将其包含在 linux PATH 变量中。

If you start the python installation again, you should select the option Change python and in the next step you can "Add python.exe to Path". Next time you open GIT Bash, the path is correct.

如果您再次开始安装python,您应该选择更改python选项,然后在下一步中您可以“将python.exe添加到路径”。下次打开 GIT Bash 时,路径是正确的。

回答by Kean Amaral

Here is the SOLUTION

这是解决方案

If you get Response:

如果你得到响应:

  1. bash: python: command not foundOR
  2. bash: conda: command not found
  1. bash: python: command not found或者
  2. bash: conda: command not found

To the following Commands:when you execute pythonor python -Vcondaor conda --versionin your Git/Terminal window

到以下命令:当你执行pythonpython -Vcondaconda --version在你的 Git/终端窗口中

Background:This is because you either

背景:这是因为你要么

  1. Installed Python in a location on your C Drive (C:) which is not directly in your program files folder.
  2. Installed Python maybe on the D Drive (D:) and your computer by default searches for it on your C:
  3. You have been told to go to your environment variables (located if you do a search for environment variables on your machines start menu) and change the "Path" variable on your computer and this still does not fix the problem.
  1. 将 Python 安装在 C 驱动器 (C:) 上的某个位置,该位置不直接位于程序文件文件夹中。
  2. 安装的 Python 可能在 D 盘 (D:) 上,并且您的计算机默认在 C 盘上搜索它:
  3. 您已被告知转到环境变量(如果您在计算机开始菜单上搜索环境变量,则位于)并更改计算机上的“路径”变量,这仍然不能解决问题。

Solution:

解决方案:

  1. At the command prompt, paste this command export PATH="$PATH:/c/Python36". That will tell Windows where to find Python. (This assumes that you installed it in C:\Python36)

  2. If you installed python on your D drive, paste this command export PATH="$PATH:/d/Python36".

  3. Then at the command prompt, paste pythonor python -Vand you will see the version of Python installed and now you should not get Python 3.6.5

  4. Assuming that it worked correctly you will want to set up git bash so that it always knows where to find python. To do that, enter the following command: echo 'export PATH="$PATH:/d/Python36"' > .bashrc

  1. 在命令提示符处,粘贴此命令export PATH="$PATH:/c/Python36"。这将告诉 Windows 在哪里可以找到 Python。(这里假设你安装在 C:\Python36 中)

  2. 如果您在 D 盘上安装了 python,请粘贴此命令export PATH="$PATH:/d/Python36"

  3. 然后在命令提示符下,粘贴pythonpython -V,你会看到安装的 Python 版本,现在你不应该得到Python 3.6.5

  4. 假设它正常工作,您将需要设置 git bash 以便它始终知道在哪里可以找到 python。为此,请输入以下命令:echo 'export PATH="$PATH:/d/Python36"' > .bashrc

Permanent Solution

永久解决方案

  1. Go to BASH RC Source File (located on C: / C Drive in “C:\Users\myname”)

  2. Make sure your BASH RC Source File is receiving direction from your Bash Profile Source File, you can do this by making sure that your BASH RC Source File contains this line of code: source ~/.bash_profile

  3. Go to BASH Profile Source File (located on C: / C Drive in “C:\Users\myname”)

  4. Enter line: export PATH="$PATH:/D/PROGRAMMING/Applications/PYTHON/Python365" (assuming this is the location where Python version 3.6.5 is installed)

  5. This should take care of the problem permanently. Now whenever you open your Git Bash Terminal Prompt and enter “python” or “python -V” it should return the python version

  1. 转到 BASH RC 源文件(位于 C:/C 驱动器中的“C:\Users\myname”)

  2. 确保您的 BASH RC 源文件正在接收来自您的 Bash 配置文件源文件的指示,您可以通过确保您的 BASH RC 源文件包含以下代码行来做到这一点: source ~/.bash_profile

  3. 转到 BASH 配置文件源文件(位于 C:/C 驱动器中的“C:\Users\myname”)

  4. 输入行:export PATH="$PATH:/D/PROGRAMMING/Applications/PYTHON/Python365"(假设这是安装Python 3.6.5版本的位置)

  5. 这应该可以永久解决问题。现在,每当您打开 Git Bash 终端提示并输入“ python”或“ python -V”时,它都应该返回 python 版本

回答by The Coder

Tried multiple of these, I switched to Cygwin instead which fixed python and some other problems I was having on Windows:

尝试了其中的多个,我改用 Cygwin 来修复 python 和我在 Windows 上遇到的其他一些问题:

https://www.cygwin.com/

https://www.cygwin.com/