0

I'm currently trying to add a git extension (or plug-in, whatever you want to call it) to the CLI on windows, so if I typed git hello, it would execute a hello world program for example.

The hello world program (in python) is below. This works perfectly fine on Unix systems.

#!/usr/bin/env python print ("Hello GIT!")

If I put the file in my path, and name it "git-hello" it should execute when I call "git hello". The file doesn't have an extension as it'd make the command "git hello.py" which is undesirable and also fails in the same way.

However, GIT is having a bit of trouble with the environment line and spits out the following:

PS D:\GitTests> git hello /usr/bin/env: 'python': No such file or directory


Things I've tried are:

  1. Changing "python" to be "py", as that's how my python interpreter launches on the CLI. This just causes the entire thing to hang and do nothing.
  2. Changing the environment line to be the direct path to the executable for python.

I know that you can use the git config method (so it'd be git config --global alias.hello "!py D:/GitTests/git-hello.py" in my case), however I don't think that seems like the best way to do things, as on windows you need supply the python interpreter in the alias.

So my question is - How do you get python programs (such as the above one) to execute with GIT on Windows?

Edit: The suggested duplicate does not address the problem here as it was to do with file association on the command line, but the issue for this question was that python was not correctly in the path.

For those encountering similar issues and installed python via 2017 - ensure your path is correct. VS Installer did not add python to my path, but instead some variant that had only py.exe and not python.exe in the path.

Liam Kelly
  • 231
  • 3
  • 15
  • Which version of python are you using? – Roland Smith Nov 04 '17 at 21:31
  • 1
    Possible duplicate of [How to execute Python scripts in Windows?](https://stackoverflow.com/questions/1934675/how-to-execute-python-scripts-in-windows) – phd Nov 04 '17 at 21:52
  • @RolandSmith I'm using 3.6.2. However I've heard that python 3 isn't necessarily great because it broke a lot of things, or something like that? Could this be the issue? – Liam Kelly Nov 05 '17 at 09:25

1 Answers1

1

It's not a Python 3 issue, I think. IMO, Python 3 is a large improvment over Python 2. Also, from the official source: Python 2.x is legacy, Python 3.x is the present and future of the language.

I think the problem lies with git. Let me elaborate.

Both Python (see PEP 397) and git (see git hooks) have brought the UNIX #! line mechanism for launching scripts to ms-windows.

The Python implementation just tries to determine which Python version you want, and launches that.

AFAICT, the git version (which is built on msys these days) just tries to execute the command. So when your script contains #!/usr/bin/env python it runs its built-in version of the env command, which tries to find python.

That will only work if the location of python.exe is in the PATH environment variable. Which does not seem to be the case, because env can't find it. (Which is odd, because I thought the Python installer takes care of this for you.)

According to the section "Environment Settings" on this page, you should only modify the user path when using msys, not the system-wide path. I would suggest that you use the method described there to add the location of your Python executable to the user path. While you're at it, I would suggest to also add the location of the Python Scripts directory to your path. That is generally useful.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • The problem was that my python wasn't on my path correctly. I installed Python 3 via the Visual Studio Installer as that's what I'll be developing with so it made sense. However, "py" is put on the path vs python its self. I assumed that Visual Studio would have done this correctly but apparently it has its a slightly non-standard way of setting things up. – Liam Kelly Nov 05 '17 at 12:52
  • 1
    For ms-windows users I generally recommend a Python distribution like Anaconda because they have an excellent package menagement. *Especially* when you need extension modules that contain C code that needs to be compiled into a shared library like numpy, pillow or matplotlib. Building such modules yourself on ms-windows is a huge pain in the ass. – Roland Smith Nov 05 '17 at 13:04