2011-02-08 47 views
1

由於python傳遞使用版本3作爲默認值,所以需要使用corret python解釋器來處理version2代碼的執行。我有一個小型的python2項目,我使用make來配置和安裝Python包,所以這裏是我的問題:我如何確定Makefile內的python版本?如何確定Makefile中的python版本?

這是我想使用的邏輯:如果 (python.version == 3)python2 some_script.py2 其他python3 some_script.py3

提前感謝!

回答

3
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 
0

這可能有助於:SF中的here's a thread關於檢查Python版本以控制新的語言功能。

+0

良好的使用它,但我真的需要處理它的Makefile ... – Rizo 2011-02-08 13:03:32

+0

內。在這種情況下,看這裏:HTTP:/ /old.nabble.com/Detecting-which-Python-version-is-used-by-SIP-and-PyQt-td30033430.html – 2011-02-08 13:09:18

3

這是一個較短的解決方案。在你的makefile寫的頂部:

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

然後你就可以用$(PYV)