Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detemine python version in Makefile?

Since python passed to use version 3 as the default there's a need to handle the version2 code execution with the corret python interpreter. I have a small python2 project where I use make to configure and install python package, so here's my question: How can I determine python's version inside Makefile?

Here's the logic I want to use: if (python.version == 3) python2 some_script.py2 else python3 some_script.py3

Thanks in advance!

like image 906
Rizo Avatar asked Feb 08 '11 12:02

Rizo


3 Answers

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
like image 178
Maxim Egorushkin Avatar answered Nov 10 '22 18:11

Maxim Egorushkin


Here is a shorter solution. in top of your makefile write:

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

Then you can use it with $(PYV)

like image 20
Farshid Ashouri Avatar answered Nov 10 '22 18:11

Farshid Ashouri


Here we check for python version > 3.5 before continuing to run make recipes

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
like image 6
mosh Avatar answered Nov 10 '22 18:11

mosh