[CMake] Finding Python3

Eric Noulard eric.noulard at gmail.com
Sun Jul 18 18:47:54 EDT 2010

2010/7/18 Alan W. Irwin <irwin at beluga.phys.uvic.ca>:

> # Get the Python version.
>  execute_process(
>    ${PYTHON_EXECUTABLE} -c "import sys; print sys.version.split()[0]"
>    OUTPUT_VARIABLE PYTHON_version_output
>    )
>  SET(PYTHON_VERSION ${PYTHON_version_output} CACHE STRING "Python version")
> (We use this approach currently in PLplot.)  One caveat is the result of
> the above sometimes has trailing information that needs to be trimmed
> off.  For example, on my current system the result is "2.6.5+".

"import sys; print sys.version.split()[0]" will not work with python 3 because
print is a function:

so you'd rather use:

"import sys; print(sys.version.split()[0])"

Now you can avoid the trailing informations if you use "sys.version_info"
instead of "sys.version".

On 2.6.5+ you get something like:

>>> print(sys.version_info)
(2, 6, 5, 'final', 0)

while on 3.1.x
>>> print (sys.version_info)
sys.version_info(major=3, minor=1, micro=2, releaselevel='final', serial=0)

using separate


should work both with python 2.[56].x and 3.x.y.

Membre de l'April - « promouvoir et défendre le logiciel libre » -

More information about the CMake mailing list