python 在python中调用subprocess.Popen时“系统找不到指定的文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:25:37  来源:igfitidea点击:

"The system cannot find the file specified" when invoking subprocess.Popen in python

pythonsvn-merge

提问by Hobo

I'm trying to use svnmerge.py to merge some files. Under the hood it uses python, and when I use it I get an error - "The system cannot find the file specified". Colleagues at work are running the same version of svnmerge.py, and of python (2.5.2, specifically r252:60911) without an issue.

我正在尝试使用 svnmerge.py 来合并一些文件。在引擎盖下它使用 python,当我使用它时,我收到一个错误 - “系统找不到指定的文件”。工作中的同事正在运行相同版本的 svnmerge.py 和 python(2.5.2,特别是 r252:60911),没有任何问题。

I found this link, which describes my problem. Trying what was outlined there, I confirmed Python could find svn (it's in my path):

我找到了这个链接,它描述了我的问题。尝试那里概述的内容,我确认 Python 可以找到 svn(它在我的路径中):

P:\>python 
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import os 
>>> i,k = os.popen4("svn --version") 
>>> i.close() 
>>> k.readline() 
'svn, version 1.4.2 (r22196)\n' 

Looking at the svnmerge.py code, though, I noticed for python versions 2.4 and higher it was following a different execution path. Rather than invoking os.popen4() it uses subprocess.Popen(). Trying that reproduces the error:

但是,查看 svnmerge.py 代码,我注意到 Python 2.4 及更高版本遵循不同的执行路径。它使用 subprocess.Popen() 而不是调用 os.popen4()。尝试重现错误:

C:\>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> p = subprocess.Popen("svn --version", stdout=subprocess.PIPE, 
>>> close_fds=False, stderr=subprocess.PIPE)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\subprocess.py", line 594, in __init__
    errread, errwrite)
  File "C:\Python25\lib\subprocess.py", line 816, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>

For now, I've commented out the 2.4-and-higher specific code, but I'd like to find a proper solution.

现在,我已经注释掉了 2.4 及更高版本的特定代码,但我想找到一个合适的解决方案。

If it's not obvious, I'm a complete python newbie, but google hasn't helped. Any pointers?

如果不是很明显,我是一个完整的 python 新手,但谷歌没有帮助。任何指针?

回答by dF.

It's a bug, see the documentation of subprocess.Popen. There either needs to be a "shell=True" option, or the first argument needs to be a sequence ['svn', '--version']. As it is now, Popenis looking for an executable named, literally, "svn --version" which it doesn't find.

这是一个错误,看到的文档subprocess.Popen。要么需要一个"shell=True“选项,要么第一个参数需要是一个序列['svn', '--version']。就像现在一样,Popen正在寻找一个名为“svn --version”的可执行文件,但它没有找到。

I don't know why it would work for your colleagues though, if they are running the same OS and version of Python... FWIW it gives me the same error message on a mac, and either of the two ways I gave fixes it.

我不知道为什么它对你的同事有用,如果他们运行相同的操作系统和 Python 版本...... FWIW 它在 Mac 上给我相同的错误消息,我提供的两种方法中的任何一种都修复了它.