[CMake] Problems with FindPythonInterp

Yngve Inntjore Levinsen yngve.levinsen at gmail.com
Tue Oct 11 07:50:37 EDT 2011


Fredag 07 oktober 2011 skrev Yngve Inntjore Levinsen:
> Dear all,
> 
> I have some configuration scripts in python that needs to be run for my 
> project, so I use the FindPythonInterp package. This has nicely found the 
> version 2.7 on my system so far, but now it suddenly finds /usr/bin/python 
> which is a soft link to version 3.2. My script doesn't work with python 3.
> 
> First: Is it possible to set a requirement that it should be python version 
2?
> 
> Second: This for some weird reason only happens on my desktop, even though I 
> have pretty much exactly the same setup on my laptop. On my laptop it finds
> /usr/bin/python2.7 (as it always has). They both have the same python 
versions 
> installed (2.7 and 3.2), and they both have the same cmake version installed 
> (2.8.6). I am running Arch Linux.
> 
> On my laptop:
> -- Found PythonInterp: /usr/bin/python2.7
> On my desktop:
> -- Found PythonInterp: /usr/bin/python (found version "3.2.2")
> 
> I'm sure there must be some differences in what's installed on the two 
systems,
> but I cannot figure out what.
> 
> I tried 
> find_package(PythonInterp 2.7 REQUIRED)
> but that still found the 3.2 version and only commented that 2.7 was 
required.
> 
> Let me know if I can provide further information.
> 
> Cheers,
> Yngve

Hi,

I did not receive any response to my mail. Here is the solution which was the 
best I could come up with:

First, I made a copy of each of the python scripts and gave them new names 
with "_py3" at the end. I converted those to python 3 syntax (2to3 helps you 
quite a bit to get started). Then I added the following in my cmake script:

if(PYTHON_VERSION_MAJOR)
    if(${PYTHON_VERSION_MAJOR} EQUAL 3)
        set(PYSCRPT_END "_py3")
    endif()
endif()

Finally I added ${PYSCRPT_END} to all python script names where they occured 
in the cmake scripts. That way cmake chooses the correct scripts 
automatically, which is a good enough solution for me.

I would say though, I'd recommend that you create a way to require python 2. 
It isn't as easy for everyone to rewrite their code, some needed libraries 
might be missing. It is stupid if cmake cannot be used on systems where both 
python 2 and python 3 are installed for this rather simple reason.

Cheers,
Yngve


More information about the CMake mailing list