Make subprocess find git executable on Windows(使子进程 find git 在 Windows 上可执行)
问题描述
import subprocess
proc = subprocess.Popen('git status')
print 'result: ', proc.communicate()
我的系统路径中有 git,但是当我像这样运行子进程时,我得到:WindowsError: [错误2]系统找不到指定的文件
I have git in my system path, but when I run subprocess like this I get:
WindowsError: [Error 2] The system cannot find the file specified
如何让子进程在系统路径中找到 git?
How can I get subprocess to find git in the system path?
Windows XP 上的 Python 2.6.
Python 2.6 on Windows XP.
推荐答案
这里看到的问题是 Windows API 函数 CreateProcess,由子进程在底层使用,不会自动解析除 .exe之外的其他可执行扩展代码>.在 Windows 上,'git' 命令实际上是作为
git.cmd
安装的.因此,您应该修改您的示例以显式调用 git.cmd
:
The problem you see here is that the Windows API function CreateProcess, used by subprocess under the hood, doesn't auto-resolve other executable extensions than .exe
. On Windows, the 'git' command is really installed as git.cmd
. Therefore, you should modify your example to explicitly invoke git.cmd
:
import subprocess
proc = subprocess.Popen('git.cmd status')
print 'result: ', proc.communicate()
git
在 shell==True
时起作用的原因是 Windows shell 自动将 git
解析为 git.cmd代码>.
The reason git
works when shell==True
is that the Windows shell auto-resolves git
to git.cmd
.
import subprocess
import os.path
def resolve_path(executable):
if os.path.sep in executable:
raise ValueError("Invalid filename: %s" % executable)
path = os.environ.get("PATH", "").split(os.pathsep)
# PATHEXT tells us which extensions an executable may have
path_exts = os.environ.get("PATHEXT", ".exe;.bat;.cmd").split(";")
has_ext = os.path.splitext(executable)[1] in path_exts
if not has_ext:
exts = path_exts
else:
# Don't try to append any extensions
exts = [""]
for d in path:
try:
for ext in exts:
exepath = os.path.join(d, executable + ext)
if os.access(exepath, os.X_OK):
return exepath
except OSError:
pass
return None
git = resolve_path("git")
proc = subprocess.Popen('{0} status'.format(git))
print 'result: ', proc.communicate()
这篇关于使子进程 find git 在 Windows 上可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使子进程 find git 在 Windows 上可执行


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01