Registry / data / pybind11

pybind11

JSON →
library3.1.0pypypi✓ verified 27d ago

pybind11 is a lightweight, header-only library that provides seamless operability between C++11 (and newer) and Python. It's primarily used to create Python bindings for existing C++ code, minimizing boilerplate through compile-time introspection. The library is actively maintained (current version 3.0.3) with frequent bug fixes and regular major releases that often introduce ABI bumps. It supports CPython 3.8+, PyPy3 7.3.17+, and GraalPy 24.1+.

pip install pybind11
INSTALL
IMPORT
SIG · PYBIND11
P
pybind11
datapythonv3.1.0
Install
1.6s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

get_include
from pybind11 import get_include
import your_cpp_module
get_cmake_dir
from pybind11 import get_cmake_dir
commands
from pybind11 import commands

A minimal example demonstrating how to create a C++ function, expose it to Python using `PYBIND11_MODULE`, and then import and use the generated module from Python. The build command illustrates manual compilation on Unix-like systems, though `setuptools` or `CMake` are typically used for larger projects.

/* example.cpp */ #include <pybind11/pybind11.h> namespace py = pybind11; int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example plugin"; // optional module docstring m.def("add", &add, "A function which adds two numbers"); } # Python (in the same directory or after installation) import example result = example.add(1, 2) print(f"The result is: {result}") # Expected: The result is: 3 # To build from source (Unix-like systems): # Ensure C++ compiler (e.g., g++), Python dev headers, and pybind11 are available. # c++ -O3 -Wall -shared -std=c++11 -fPIC \ # $(python3 -m pybind11 --includes) example.cpp \ # -o example$(python3 -m pybind11 --extension-suffix)
Debug
Known issues
breakingpybind11 v3.0.0 introduced an ABI bump. Extensions built with v3.0.0 or later are not ABI-compatible with those built using v2.x versions (e.g., v2.13).
fix
Rebuild all pybind11-based extensions with pybind11 v3.0.0 or later to ensure compatibility across modules.
affects: 3.0.0+
breakingSupport for older Python and CMake versions was removed in v3.0.0. Specifically, Python 3.7, PyPy 3.8/3.9, and CMake < 3.15 are no longer supported.
fix
Ensure your build environment uses Python 3.8+ (or supported PyPy/GraalPy versions) and CMake 3.15+ when upgrading to pybind11 v3.0.0+.
affects: 3.0.0+
breakingIn v3.0.0, CMake support defaults to the modern `FindPython` module. Projects using older `PYTHON_*` variables may find them ignored or deprecated.
fix
Switch to using `Python_*` variables in your `CMakeLists.txt` for Python detection instead of `PYTHON_*`. Refer to the pybind11 upgrade guide.
affects: 3.0.0+
deprecatedThe `py::enum_` API for exposing C++ enumerations is deprecated in favor of `py::native_enum`.
fix
Migrate to `py::native_enum` for improved integration with Python's `enum` system. While `py::enum_` is still present, it may be removed in a future 3.x release.
affects: 3.0.0+
gotchaImproper Global Interpreter Lock (GIL) management is a common source of bugs. Avoid invoking Python functions in global static contexts or having global pybind11 objects.
fix
Consult the pybind11 documentation on GIL management, use `py::gil_scoped_acquire` or `py::gil_scoped_release` as needed, and consider lazy initialization for global pybind11 objects.
affects: All versions
gotchaWhen working with sub-interpreters (enabled by default in v3.0.0), avoid sharing Python objects across different sub-interpreters and minimize global/static C++ state in your modules.
fix
Design your C++ modules to be stateless or carefully manage state per interpreter. Initialization functions (`PYBIND11_MODULE`) will run for each interpreter.
affects: 3.0.0+
gotchaWhen casting from a raw `PyObject*` to a `py::object` subclass (e.g., `py::str`), omitting the template argument to `py::cast` will silently result in incorrect behavior.
fix
Always use `py::cast<TargetType>(py_object_ptr)` with the explicit template argument for safe conversion.
affects: All versions
gotchaThe test script being executed is C++ code, not Python code, leading to a `SyntaxError: invalid syntax`. This indicates a fundamental misconfiguration in the test environment rather than a `pybind11`-specific issue.
fix
Ensure that the Python interpreter is running a valid Python script. If C++ code needs to be tested, it must be properly compiled, linked, and then imported/called from a Python script.
affects: All versions
Upgrade
Version history
3.1.0latest on PyPI · released Aug 6, 2026
Audit
Dependencies
numpyoptionalRequired for advanced NumPy array handling (e.g., `py::array_t`, `py::vectorize`).
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
2
Resources