ImportError: Python version mismatch

Hi, I use pybind11 to build a lib xxx.so in Ubuntu with python3.7, but this can not run with python3.9 with the below error message:
ImportError: Python version mismatch: module was compiled for Python 3.7, but the interpreter version is incompatible: 3.9.0

Does python3.9 not compatible with python3.7?

When a package is compiled, that means it is partially written in a compiled language, like C. CPython’s C API and ABI are not guaranteed to be stable across releases, see C API Stability — Python 3.10.6 documentation. Therefore, a package using the C API must be compiled against the same interpreter version as it is being used with.

It is possible to ensure compatibility with any cpython3.x interpreter by only using the Limited API. This way, the package can be compiled against any interpreter version and used with any other, without the need to recompile. Your lib xxx apparently does not do this.

On a side note, the python language itself does not guarantee backward or forward compatibility across minor versions (e.g. 3.7 to 3.8). While forward compatibility is almost always maintained, it can be broken if necessary. A recent example is the ability to chain the @property and @classmethod decorators, introduced in 3.8 and removed in 3.11 because it was causing bugs.

3.9 will run pretty much any 3.7 source code, but the important word in the error is compiled. The byte code to which Python gets compiled changes from one minor version to the next (3.9 is different from 3.8 is different from 3.7).

I don’t know pybind11, but I’m guessing it compiles your Python source and wraps it into a loadable library. You would need to build that library for each target minor version you support.

1 Like