[CMake] Behaviour of FindPythonInterp appears suboptimal

David Cole dlrdave at aol.com
Fri Aug 22 07:24:43 EDT 2014


>> I thought using FindPythonInterp would be more portable than
>> simply invoking "python" directly (i.e. using $PYTHON_EXECUTABLE).

> In case you want the newest version of any Python major, as in your
> case, try
> find_package(PythonInterp 2 REQUIRED). But you are right, this would
> also successfully return you a 2.5.

Or, if you want to prefer the python in the environment PATH, and then
do your own version check to make sure it's suitable, use something
like this:

    # http://www.cmake.org/cmake/help/v3.0/command/find_program.html
    find_program(PYTHON_EXECUTABLE python
      PATHS ENV PATH         # look in the PATH environment variable
      NO_DEFAULT_PATH           # do not look anywhere else...
      )

    find_package(PythonInterp)

And then after the find_package, you can do your own version check with
the variables:

    PYTHON_VERSION_MAJOR='2'
    PYTHON_VERSION_MINOR='7'
    PYTHON_VERSION_PATCH='6'
    PYTHON_VERSION_STRING='2.7.6'

and the VERSION_GREATER / VERSION_LESS arguments to the "if" command.


HTH,
David C.





More information about the CMake mailing list