Skip to content

Commit ef13fb2

Browse files
yesintwjakob
authored andcommitted
Info about inconsistent detection of Python version between pybind11 … (#1093)
* Info about inconsistent detection of Python version between pybind11 and CMake in FAQ
1 parent f424518 commit ef13fb2

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

docs/faq.rst

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,39 @@ that that were ``malloc()``-ed in another shared library, using data
242242
structures with incompatible ABIs, and so on. pybind11 is very careful not
243243
to make these types of mistakes.
244244

245+
Inconsistent detection of Python version in CMake and pybind11
246+
==============================================================
247+
248+
The functions ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` provided by CMake
249+
for Python version detection are not used by pybind11 due to unreliability and limitations that make
250+
them unsuitable for pybind11's needs. Instead pybind provides its own, more reliable Python detection
251+
CMake code. Conflicts can arise, however, when using pybind11 in a project that *also* uses the CMake
252+
Python detection in a system with several Python versions installed.
253+
254+
This difference may cause inconsistencies and errors if *both* mechanisms are used in the same project. Consider the following
255+
Cmake code executed in a system with Python 2.7 and 3.x installed:
256+
257+
.. code-block:: cmake
258+
259+
find_package(PythonInterp)
260+
find_package(PythonLibs)
261+
find_package(pybind11)
262+
263+
It will detect Python 2.7 and pybind11 will pick it as well.
264+
265+
In contrast this code:
266+
267+
.. code-block:: cmake
268+
269+
find_package(pybind11)
270+
find_package(PythonInterp)
271+
find_package(PythonLibs)
272+
273+
will detect Python 3.x for pybind11 and may crash on ``find_package(PythonLibs)`` afterwards.
274+
275+
It is advised to avoid using ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` from CMake and rely
276+
on pybind11 in detecting Python version. If this is not possible CMake machinery should be called *before* including pybind11.
277+
245278
How to cite this project?
246279
=========================
247280

@@ -256,4 +289,3 @@ discourse:
256289
note = {https://github.com/pybind/pybind11},
257290
title = {pybind11 -- Seamless operability between C++11 and Python}
258291
}
259-

0 commit comments

Comments
 (0)