简体   繁体   English

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

[英]Git Bash won't run my python files?

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. 我一直试图在Git Bash中运行我的python文件,但是我一直遇到错误,无法弄清楚如何修复它。 My command as follows in the git bash executable python filename.py then it says 我的命令在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。

Adapting the PATH should work. 修改PATH应该起作用。 Just tried on my Git bash: 刚刚尝试了我的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 .exe on the PATH ; 不要在PATH上指定.exe and use slashes. 并使用斜杠。

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

Here is the SOLUTION 这是解决方案

If you get Response: 如果收到响应:

  1. bash: python: command not found OR bash: python: command not found
  2. bash: conda: command not found

To the following Commands: when you execute python or python -V conda or conda --version in your Git/Terminal window 执行以下命令:在Git / Terminal窗口中执行pythonpython -V condaconda --version

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. 将Python安装在C驱动器(C :)上的某个位置,该位置不直接位于程序文件文件夹中。
  2. Installed Python maybe on the D Drive (D:) and your computer by default searches for it on your C: 安装的Python也许在D盘(D :)上,并且您的计算机默认在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. 有人告诉您转到环境变量(如果在计算机的“开始”菜单上搜索环境变量,则位于此位置)并更改计算机上的“路径”变量,但这仍然不能解决问题。

Solution: 解:

  1. At the command prompt, paste this command export PATH="$PATH:/c/Python36" . 在命令提示符处,粘贴此命令export PATH="$PATH:/c/Python36" That will tell Windows where to find Python. 这将告诉Windows在哪里可以找到Python。 (This assumes that you installed it in C:\\Python36) (这假定您将其安装在C:\\ Python36中)

  2. If you installed python on your D drive, paste this command export PATH="$PATH:/d/Python36" . 如果在D驱动器上安装了python,请粘贴此命令export PATH="$PATH:/d/Python36"

  3. Then at the command prompt, paste python or python -V and you will see the version of Python installed and now you should not get Python 3.6.5 然后在命令提示符下,粘贴pythonpython -V ,您将看到安装的Python版本,现在不应该获取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. 假设它正常工作,您将需要设置git bash,以便它始终知道在哪里可以找到python。 To do that, enter the following command: echo 'export PATH="$PATH:/d/Python36"' > .bashrc 为此,请输入以下命令: 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”) 转到BASH RC源文件(位于“ C:\\ Users \\ myname”中的C:/ C驱动器上)

  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 确保您的BASH RC源文件从Bash配置文件源文件接收指令,可以通过确保BASH RC源文件包含以下代码行来执行此操作:source〜/ .bash_profile

  3. Go to BASH Profile Source File (located on C: / C Drive in “C:\\Users\\myname”) 转到BASH配置文件源文件(位于“ C:\\ Users \\ myname”中的C:/ C驱动器上)

  4. Enter line: export PATH="$PATH:/D/PROGRAMMING/Applications/PYTHON/Python365" (assuming this is the location where Python version 3.6.5 is installed) 输入以下行:export PATH =“ $ PATH:/ D / PROGRAMMING / Applications / PYTHON / Python365”(假设这是安装Python 3.6.5版的位置)

  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 现在,无论何时打开Git Bash终端提示符并输入“ python ”或“ python -V ”,它都应返回python版本

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

屏幕截图

Add following line in you .bashrc file 在您的.bashrc file添加以下行

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

When you install python for windows, there is an option to include it in the path. 当您为Windows安装python时,有一个选项可以将其包括在路径中。 For python 2 this is not the default. 对于python 2,这不是默认设置。 It adds the python installation folder and script folder to the Windows path. 它将python安装文件夹和脚本文件夹添加到Windows路径。 When starting the GIT Bash command prompt, it have included it in the linux PATH variable. 启动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". 如果再次开始安装python,则应选择“更改python”选项,然后在下一步中可以“将python.exe添加到路径”。 Next time you open GIT Bash, the path is correct. 下次打开GIT Bash时,路径正确。

Tried multiple of these, I switched to Cygwin instead which fixed python and some other problems I was having on Windows: 尝试了这些的倍数,我改用Cygwin,它修复了Windows上的python和其他一些问题:

https://www.cygwin.com/ https://www.cygwin.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM