简体   繁体   English

如何在Windows中执行Python脚本?

[英]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. 我目前正在尝试在Windows的CLI上添加git扩展(或插件,无论您要调用什么),因此,如果我输入git hello ,它将执行例如hello world程序。

The hello world program (in python) is below. 下面是hello world程序(在python中)。 This works perfectly fine on Unix systems. 这在Unix系统上可以很好地工作。

#!/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". 如果将文件放在路径中,并将其命名为“ git-hello”,则应在调用“ 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. 该文件没有扩展名,因为它会使命令“ git hello.py”不受欢迎,并且同样会失败。

However, GIT is having a bit of trouble with the environment line and spits out the following: 但是,GIT在环境方面遇到了一些麻烦,并吐出了以下内容:

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. 将“ python”更改为“ py”,因为这就是我的python解释器在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. 将环境行更改为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. 我知道您可以使用git config方法(因此我使用的是git config --global alias.hello“!py D:/GitTests/git-hello.py” ),但是我不认为似乎是最好的处理方式,因为在Windows上,您需要在别名中提供python解释器。

So my question is - How do you get python programs (such as the above one) to execute with GIT on Windows? 所以我的问题是-如何在Windows上使用GIT执行python程序(例如上述程序)?

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. 编辑:建议的重复项无法解决此问题,因为它与命令行上的文件关联有关,但是此问题的问题是python的路径不正确。

For those encountering similar issues and installed python via 2017 - ensure your path is correct. 对于那些遇到类似问题并在2017年安装python的人-确保您的路径正确。 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. VS Installer并未将python添加到我的路径中,而是在路径中仅包含py.exe而没有python.exe的某些变体。

It's not a Python 3 issue, I think. 我认为这不是Python 3问题。 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. IMO,Python 3是对Python 2的重大改进。此外,从官方消息来看: Python 2.x是旧版,Python 3.x是该语言的现在和将来。

I think the problem lies with git. 认为问题出在git。 Let me elaborate. 让我详细说明。

Both Python (see PEP 397 ) and git (see git hooks ) have brought the UNIX #! Python(请参阅PEP 397 )和git(请参阅git hooks )都带来了UNIX #! line mechanism for launching scripts to ms-windows. 用于向MS-Windows启动脚本的行机制。

The Python implementation just tries to determine which Python version you want, and launches that. Python实现只是尝试确定所需的Python版本,然后启动它。

AFAICT, the git version (which is built on msys these days) just tries to execute the command. AFAICT的git版本( 最近msys构建 )只是尝试执行命令。 So when your script contains #!/usr/bin/env python it runs its built-in version of the env command, which tries to find python . 因此,当您的脚本包含#!/usr/bin/env python它将运行其内置版本的env命令,该命令会尝试查找python

That will only work if the location of python.exe is in the PATH environment variable. 仅当python.exe的位置在PATH环境变量中时,此方法才有效。 Which does not seem to be the case, because env can't find it. 事实并非如此,因为env找不到它。 (Which is odd, because I thought the Python installer takes care of this for you.) (这很奇怪,因为我认为Python安装程序会为您解决这个问题。)

According to the section "Environment Settings" on this page , you should only modify the user path when using msys, not the system-wide path. 根据此页面上的“环境设置”部分,您仅应在使用msys时修改用户路径,而不是系统范围的路径。 I would suggest that you use the method described there to add the location of your Python executable to the user path. 我建议您使用此处描述的方法将Python可执行文件的位置添加到用户路径。 While you're at it, I would suggest to also add the location of the Python Scripts directory to your path. 当您使用它时,我建议您还将Python Scripts目录的位置添加到您的路径中。 That is generally useful. 这通常是有用的。

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

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