Search code examples
cmakepybind11

CMake and pybind11 using inconsistent Python Versions


I am creating a starter project with CMake (3.16.3) and pybind11 (2.4.3) in VSCode (1.46.1) on Ubuntu (20.04) which has both Python 2.7 and 3.8 on it by default. I want to build a module for Python3. When I use the following two lines in my CMakeLists.txt

find_package(pybind11)
find_package(Python COMPONENTS Interpreter Development REQUIRED)

The CMake configuration is

[cmake] -- Found PythonInterp: /usr/bin/python (found version "2.7.18")  
[cmake] -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so  
[cmake] -- Found Python3: /usr/bin/python3.8 (found version "3.8.2") found components: Interpreter Development 

Switching the order of the find_package statements

find_package(Python COMPONENTS Interpreter Development REQUIRED)
find_package(pybind11)

Gives the same python links but with the new order

[cmake] -- Found Python: /usr/bin/python3.8 (found version "3.8.2") found components: Interpreter Development 
[cmake] -- Found PythonInterp: /usr/bin/python (found version "2.7.18") 
[cmake] -- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so

I am new to this. I have read in the FAQ about inconsistent versions but I think I'm doing all the right things (I do not call find_package(PythonInterp) nor find_package(PythonLibs) but rather stick to find_package(Python)). That seems to be working, it seems find_package(pybind11) is defaulting to python2.7 (incorrectly if I understand the documents) and I do not know how set it. I have tried things like # set(bindings_python_version 3.8) but this does not change anything.

I have this working on a Windows based machine but that only has one version of Python on it so there is no chance for confusion


Solution

  • So in the end, calling set(PYBIND11_PYTHON_VERSION 3.8 CACHE STRING "") before calling find_package(pybind11) solves my problem although it does not help me understand why pybind11 defaults to 2.7. If anyone can point me to an explanation, I would be very grateful.