You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Makefile提取pyproject.toml信息的变量为空是哪里出错了?

问题根因
  • Makefile执行shell命令语法错误:你直接使用$(grep ...)的写法时,Make会将grep识别为自定义Make变量进行展开,而非执行对应的shell命令,要调用shell命令必须使用shell函数,格式为$(shell 待执行命令)。
  • 正则匹配规则错误:你的pyproject.toml中name、version字段属于[tool.poetry]小节,行首存在缩进,并非直接以name/version作为行首内容,你写的^name\s*=\s*正则(^代表行首锚定)无法匹配到目标字段,返回结果为空。
修复后的Makefile代码
# 正则增加对前导空白的匹配,同时用shell函数执行命令
NAME := $(shell grep -e "^\s*name\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs)
VERSION := $(shell grep -e "^\s*version\s*=\s*" pyproject.toml | cut -d = -f 2 | xargs)

build-apg:
	docker build --target apg --tag $(NAME):$(VERSION) .
可选优化(避免其他小节同名字段干扰)

如果你想确保只提取[tool.poetry]小节下的name和version,避免其他配置小节出现同名字段导致提取错误,可以改用awk实现更精准的提取:

NAME := $(shell awk '/\[tool.poetry\]/ {flag=1; next} /^\[/ {flag=0} flag && /^\s*name/ {print $$3}' pyproject.toml | xargs)
VERSION := $(shell awk '/\[tool.poetry\]/ {flag=1; next} /^\[/ {flag=0} flag && /^\s*version/ {print $$3}' pyproject.toml | xargs)

内容的提问来源于stack exchange,提问作者Jeff Wright

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 00:27:00