如何在Makefile中确定Python版本?
由于Python现在默认使用版本3,所以我们需要用正确的Python解释器来运行版本2的代码。我有一个小的Python2项目,我用make
来配置和安装Python包。那么我的问题是:我该如何在Makefile
中判断Python的版本呢?
我想用的逻辑是:
如果(python.version == 3)就用python2来运行某个脚本(some_script.py2)
否则就用python3来运行另一个脚本(some_script.py3)
提前谢谢你!
5 个回答
8
在这里,我们会先检查一下Python的版本是否大于3.5,然后再继续运行制作配方的步骤。
ifeq (, $(shell which python ))
$(error "PYTHON=$(PYTHON) not found in $(PATH)")
endif
PYTHON_VERSION_MIN=3.5
PYTHON_VERSION=$(shell $(PYTHON) -c 'import sys; print("%d.%d"% sys.version_info[0:2])' )
PYTHON_VERSION_OK=$(shell $(PYTHON) -c 'import sys;\
print(int(float("%d.%d"% sys.version_info[0:2]) >= $(PYTHON_VERSION_MIN)))' )
ifeq ($(PYTHON_VERSION_OK),0)
$(error "Need python $(PYTHON_VERSION) >= $(PYTHON_VERSION_MIN)")
endif
10
这里有一个更简短的解决方案。在你的makefile的顶部写上:
PYV=$(shell python -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)");
然后你可以用 $(PYV)
来使用它。
12
python_version_full := $(wordlist 2,4,$(subst ., ,$(shell python --version 2>&1)))
python_version_major := $(word 1,${python_version_full})
python_version_minor := $(word 2,${python_version_full})
python_version_patch := $(word 3,${python_version_full})
my_cmd.python.2 := python2 some_script.py2
my_cmd.python.3 := python3 some_script.py3
my_cmd := ${my_cmd.python.${python_version_major}}
all :
@echo ${python_version_full}
@echo ${python_version_major}
@echo ${python_version_minor}
@echo ${python_version_patch}
@echo ${my_cmd}
.PHONY : all
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。