I'm building some C++ Python extensions for Python 3.10 (using PyBind11) but I'm finding that when trying to import these extensions I get: ImportError: Python version mismatch: module was compiled for Python 3.8, but the interpreter version is incompatible: 3.10.5
.
I have find_package(Python3 3.10 REQUIRED)
in my CMakeLists.txt
and I use -DPYTHON_EXECUTABLE:FILEPATH=$(which python)
when running cmake
. I can confirm that which python
points to a Python 3.10 executable. When running cmake
part of the output says:
Found Python3: /PATH/TO/venv/bin/python3 (found suitable version "3.10.5", minimum required is "3.10") found components: Interpreter
and there are no other mentions of finding some other Python version. The compiled files look like module.cpython-310-x86_64-linux-gnu.so
. Because of the 310
they can only be imported into a Python 3.10 interpreter sesion.
BUT, when I try importing them I get the ImportError
I mentioned above. On the other hand, if I manually rename the compiled files to module.cpython-38-x86_64-linux-gnu.so
and open up a Python 3.8 interpreter, I'm able to import.
How can I fix this? Why are all the clues suggesting that I have built the files correctly when I somehow haven't?
Note that I have already tried the solutions from the answers here.
There may have been other factors at play here (like including 3rd party CMake projects) so to fix my problem the first step was to remove those. Then I:
find_package(Python3 3.10 REQUIRED)
to find_package(Python3 3.10 REQUIRED COMPONENTS Interpreter Development)
(see here). User @Tsyvarev also somewhat alluded to this in his comment on my question.sudo apt install python3.10-dev
(I only had this for Python 3.8). If you're like me and don't understand the difference between sudo apt install python
and sudo apt install python-dev
see this answer.