Search code examples
pythoncmakepython-extensions

How to find minimum versions of python interpreter and libs using CMake


I am building a C extension using pybind11 with CMake. I used to do this with:

find_package(Python3 3.7 REQUIRED)
find_package(PythonLibs 3.7 REQUIRED)

without any issues. Now I need python 3.8 and changed it to:

find_package(PythonInterp 3.8 REQUIRED)
find_package(PythonLibs 3.8 REQUIRED)

However, despite libpython3.8.so being in the same folder as libpython3.7m.so CMake does not find it. It returns:

Could NOT find PythonLibs: Found unsuitable version "3.7.5", but required
is at least "3.8" (found /usr/lib/x86_64-linux-gnu/libpython3.7m.so)

When researching this I noticed that PytonInterp and PythonLibs are deprecated since CMake 3.12 and FindPython is preferred. So I tried:

find_package(Python3)

This finds my python 3.8 interpreter, but not my python 3.8 libraries. Hence, I still cannot use the package, as

mypy.cpython-37m-x86_64-linux-gnu.so

is based on python 3.7 libraries. How can I ensure it also finds my 3.8 libraries? As said, they are in the same folder.


Solution

  • The documentation for the FindPython module states:

    If no COMPONENTS are specified, Interpreter is assumed.

    So, yes, since you haven't specified any COMPONENTS, only the interpreter is found.

    If you want CMake to find the Python include directories and libraries, you should specify Development:

    find_package(Python3 COMPONENTS Interpreter Development)