我尝试在 macOS Mojave 上通过 make in python 构建项目,但得到以下输出:
If there are problems, cd to the src directory and run 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 auto/config.mk
touch auto/config.h
cp config.mk.dist auto/config.
Process finished with exit code 0
但是如果我尝试从终端启动 make 一切正常。 这是我的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”)
谁能帮我解决这个问题吗?
最佳答案
正如您在问题中提到的进程已完成,退出代码为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))
输出:(如果特定文件夹中没有Makefile
。您可以看到返回码不为零,因为缺少Makefile
(错误))
>>> python3 test.py .
stdout: b'make: *** No targets specified and no makefile found. Stop.\n'
stderr: None
Return code: 2
如果我在我的 .
(根)文件夹中创建一个包含以下内容的 Makefile
。
Makefile:
FOO = Hello World
all:
@echo $(FOO)
@echo $(value FOO)
输出:(Makefile
存在于特定文件夹中,并且其内容正确,因此make
成功并返回代码为零(根据您的问题,您的情况相同)。)
>>> python3 test.py .
stdout: b'Hello World\nHello World\n'
stderr: None
Return code: 0
关于python - 无法从 python subprocess.Popen 启动 make 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57986536/