44

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.py then it says

"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

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.

gturri
  • 10,843
  • 9
  • 35
  • 53
user3496571
  • 451
  • 1
  • 4
  • 4
  • 2
    Could you type `echo $PATH` and paste the results? You may be using the Windows Environment variables instead of the ones that Bash recognizes – tlehman Apr 04 '14 at 17:26
  • 4
    Your PATH should contain the directories containing your executables, not the executables themselves. – Wooble Apr 04 '14 at 17:33
  • possible duplicate of [Adding Python Path on Windows 7](http://stackoverflow.com/questions/6318156/adding-python-path-on-windows-7) –  Apr 04 '14 at 20:02

7 Answers7

77

Adapting the PATH should work. Just tried on my 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 ; and use slashes.

gturri
  • 10,843
  • 9
  • 35
  • 53
  • 2
    No it doesn't work. It is not enough to have Python executable dir in PATH. It still doesn't work – Green Jun 08 '16 at 06:43
  • 8
    @Green: Given the number of upvotes, I'd say that it might have been pretty effective so far. ie: I think you're in a particular case here (custom bash? python installed in a path with whitespaces?). Hence I'd suggest you ask a new question, so you'll be able to give as much details as possible, in order to get a more accurate reply for your particular case – gturri Jun 09 '16 at 10:30
  • this works for me. Any way to automatically do this every time I open up git bash instead of having to rewrite this every time? EDIT: other answer below explains how to add to bash rc – CoffeeTableEspresso Jun 03 '19 at 22:54
21

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.

echo 'export PATH="$PATH:/c/Python27"' > .profile
Casimir Crystal
  • 18,651
  • 14
  • 55
  • 76
8

Here is the SOLUTION

If you get Response:

  1. bash: python: command not found OR
  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

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.

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 python or python -V and 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

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

Kean Amaral
  • 3,227
  • 2
  • 12
  • 8
2

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.

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.

Håkan Nilsson
  • 135
  • 1
  • 3
0

This works great on win7

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

Screenshot

warunapww
  • 889
  • 3
  • 16
  • 36
Jasper Kinoti
  • 463
  • 3
  • 9
0

Add following line in you .bashrc file

############################
# Environment path setting #
############################
export PATH=/c/Python27:/c/Python27/Scripts:$PATH
J4cK
  • 28,400
  • 8
  • 39
  • 54
  • Yes, thank you I had created a .bashrc file in my C:\Users\. I deleted it and restarted GitBash and commands started working again. Not sure who downvoted this but appreciate you not deleting this! – Jeremy Thompson Sep 25 '19 at 02:59
0

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

https://www.cygwin.com/

The Coder
  • 3,277
  • 1
  • 24
  • 34