简体   繁体   中英

How to execute Python script in Windows?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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