python - 将值从 python 脚本返回到 makefile 变量

标签 python python-3.x python-2.7 makefile

我有一个要求,我必须通过 makefile 运行 python 脚本。 Python 脚本执行一些计算并返回一个值。我想在该 makefile 变量中获取该值。我该怎么做?

我想要这样的东西:

mymakefile.mke

$output = python process.py

进程.py

import os

def main():
    value = "01.00.00"
    return value
if __name__ == "__main__":
    main()

最佳答案

如果您想要Make 变量中的值,您可以这样做:

output := $(shell python process.py)

(注意使用:=而不是=,因为如果你使用=,Make会在每次求值时运行脚本变量。)

如果您想要配方中的 shell 变量中的值,请查看您将在 shell 命令行中使用的语法,这可能类似于:

output=$(python process.py); echo $output

但在 makefile 规则中,您必须使用更多美元符号转义美元符号:

some_target:
    output=$$(python process.py); echo $$output

请注意,由于配方的每一行都在其自己的子 shell 中执行,因此变量的赋值和使用必须在同一行;它不会存活到下一行。

关于python - 将值从 python 脚本返回到 makefile 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263526/

相关文章:

python - 403 禁止使用 Urllib2 [Python]

python - Django 1.8 中的登录测试用例失败

Python - 使用多处理和按键检测

python - pandas 查找多行中特定列中的最小值

python - 在 if 语句中比较三个变量

java - Python 有类似 Java 的 System.arraycopy 的东西吗?

python - 如何为 tensorflow 建模数据?

python - 如何创建具有相同类型成员的静态类型Python类

python - 具有列表理解的 Python 游程编码

python - conda 更新 anaconda 的问题