如何在Makefile中确定python版本?

时间:2011-02-08 12:57:44

标签: python makefile

由于python传递使用版本3作为默认值,因此需要使用corret python解释器处理版本2代码执行。我有一个小的python2项目,我使用make来配置和安装python包,所以这里是我的问题:如何在Makefile中确定python的版本?

这是我想要使用的逻辑:     if(python.version == 3)python2 some_script.py2     别的python3 some_script.py3

提前致谢!

5 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:6)

这是一个较短的解决方案。在makefile的顶部写:

PYV=$(shell python -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)");

然后您可以将其与$(PYV)

一起使用

答案 2 :(得分:0)

这可能有助于:here's a thread在SF中检查Python版本以控制新的语言功能。

答案 3 :(得分:0)

在这里,我们在继续运行制作食谱之前检查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

答案 4 :(得分:0)

PYTHON=$(shell command -v python3)

ifeq (, $(PYTHON))
    $(error "PYTHON=$(PYTHON) not found in $(PATH)")
endif

PYTHON_VERSION_MIN=3.9
PYTHON_VERSION_CUR=$(shell $(PYTHON) -c 'import sys; print("%d.%d"% sys.version_info[0:2])')
PYTHON_VERSION_OK=$(shell $(PYTHON) -c 'import sys; cur_ver = sys.version_info[0:2]; min_ver = tuple(map(int, "$(PYTHON_VERSION_MIN)".split("."))); print(int(cur_ver >= min_ver))')
ifeq ($(PYTHON_VERSION_OK), 0)
    $(error "Need python version >= $(PYTHON_VERSION_MIN). Current version is $(PYTHON_VERSION_CUR)")
endif