python - 无法从 python subprocess.Popen 启动 make 命令

标签 python python-3.x macos makefile subprocess

我尝试在 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/

相关文章:

python - 如何使用Python生成大于指定数字的随机数?

python - MySQL python 连接器 : "not all arguments converted during bytes formatting"

python - 在哪里可以找到与 hyperopt 最佳配置对应的损失?

python - 单击按钮后刷新 QML 文件中的 QQuickWidget

swift - 如何更改 NSPopUpButtonCell NSTableView Swift 4.2 中的 selectItem?

linux - Linux 和 OSX 的文件系统之间有什么区别?

html - 是否有适用于 Mac 的半途而废的免费 HTML 编辑器?

javascript - 递归函数调用如何在javascript中工作

python - 如何正确使用 Python 中的 multiprocessing 模块?

python-3.x - Pathlib mkdir 引发 FileExistsError 尽管文件实际上并不存在