Can';t从python subprocess.Popen启动make命令
我试图在macOS Mojave上用python生成项目,但我得到以下输出: 如果出现问题,请将cd刻录到src目录并运行make thereCan';t从python subprocess.Popen启动make命令,python,python-3.x,macos,makefile,subprocess,Python,Python 3.x,Macos,Makefile,Subprocess,我试图在macOS Mojave上用python生成项目,但我得到以下输出: 如果出现问题,请将cd刻录到src目录并运行make there cd src&&/Library/Developer/CommandLineTools/usr/bin/make first rm-f auto/config.status auto/config.cache config.log auto/config.log rm-f auto/config.h auto/link.log auto/link.sed
cd src&&/Library/Developer/CommandLineTools/usr/bin/make first
rm-f auto/config.status auto/config.cache config.log auto/config.log
rm-f auto/config.h auto/link.log auto/link.sed auto/config.mk
触摸自动/配置.h
cp config.mk.dist自动/配置。
进程已完成,退出代码为0 但如果我试着从终端开始,一切都很好。 下面是我的python代码:
make_command = "make"
make_proc = subprocess.Popen(make_command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=args[1])
args[1]–带项目的文件夹路径,正确(“/Users/kirill/.vim_updater”)
有人能帮我解决这个问题吗?我是个新手,但仍然没有足够的声誉来评论 所以请随时尝试一下,告诉我,这样我就可以删除这个“答案” 使用subprocess.call()而不是subprocess.Popen() 您还需要检查返回代码 我想你应该等待结果
为了进行测试,您还可以尝试使用make命令的绝对路径。正如您在问题
中所提到的,过程以退出代码0完成。这意味着您的make
命令成功。您可以尝试以下改进的代码部分
代码:
import subprocess
import sys
make_command = ["make"]
make_proc = subprocess.Popen(make_command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=sys.argv[1])
stdout, stderr = make_proc.communicate()
print("stdout: {}".format(stdout))
print("stderr: {}".format(stderr))
print("Return code: {}".format(make_proc.returncode))
FOO = Hello World
all:
@echo $(FOO)
@echo $(value FOO)
输出:(如果特定文件夹中没有Makefile
。您可以看到返回代码不是零,因为缺少Makefile
(错误))
如果我在我的
(根)文件夹中创建一个Makefile
,包含以下内容
Makefile:
import subprocess
import sys
make_command = ["make"]
make_proc = subprocess.Popen(make_command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=sys.argv[1])
stdout, stderr = make_proc.communicate()
print("stdout: {}".format(stdout))
print("stderr: {}".format(stderr))
print("Return code: {}".format(make_proc.returncode))
FOO = Hello World
all:
@echo $(FOO)
@echo $(value FOO)
输出:(特定文件夹中存在Makefile
,其内容正确,因此make
成功,返回代码为零(根据您的问题,您有相同的情况)
您有任何错误/回溯吗?为什么需要有shell=True
标志?@milanbalazs如果没有错误,流程将正确结束。唯一的标准是关于make出现问题的消息。是的,shell=True
是不需要的。但这种改进并不能解决make的问题。我有来自Make的相同输出。你能用你的输出更新你的问题吗?也许给出解决方案更容易。@milanbalazs我更新了问题,但这都是我的输出,最终的解决方案:make_proc=subprocess.call([make_命令],cwd=args[1])
应该是returncode=subprocess.call([make_命令],cwd=args[1])
通过subprocess实现。Popen
不起作用,但我是使用subprocess.Popen.call
完成的。问题是我把论点传错了位置。Thx for your answersubprocess.Popen
没有call
属性,因此您无法使用subprocess.Popen.call
。我想你的意思是子进程.call()
。另一方面,如果您对参数的位置有问题,那么.Popen
也应该可以工作。:)