pybind11: C++ and Python interoperability bindings
Header-only library creating minimal-boilerplate C++ Python bindings.
Learn more about pybind11
pybind11 is a C++ library designed to create Python bindings for existing C++ code. It operates as a header-only implementation that leverages C++11 features like tuples, lambda functions, and variadic templates to generate bindings with minimal code. The library supports CPython 3.8+, PyPy 7.3.17+, and GraalPy 24.1+, and can map C++ constructs including functions, methods, classes, exceptions, enumerations, STL containers, and smart pointers to their Python equivalents. Common use cases include wrapping performance-critical C++ libraries for use in Python applications and integrating existing C++ codebases into Python projects.
Header-only implementation
The core library consists of approximately 4,000 lines of code contained entirely in header files with no external dependencies beyond Python and the C++ standard library. This eliminates the need to link against additional libraries or manage complex build configurations.
C++11 language features
pybind11 uses modern C++ constructs to reduce boilerplate compared to earlier binding approaches. Type information is inferred at compile time through introspection, and function signatures are precomputed using constexpr, resulting in smaller binary sizes.
Broad C++ feature coverage
The library maps a wide range of C++ constructs to Python including virtual methods, multiple inheritance, custom operators, internal reference counting, and integration with NumPy arrays. It also supports C++11 lambda functions with captured variables and move semantics for efficient data transfer.
// example.cpp
#include <pybind11/pybind11.h>
int add(int a, int b) {
return a + b;
}
PYBIND11_MODULE(example, m) {
m.def("add", &add, "Add two numbers");
}
# Python usage
import example
result = example.add(5, 3)
print(result) # Output: 8Patch release fixing multiple sub-interpreter crashes and segfaults; removes subinterpreter-specific exception handling to improve stability.
- –Update if using sub-interpreters: concurrent creation now blocked in Python 3.12 to avoid stdlib concurrency bugs.
- –Fixes pointer-to-enum casting errors and module re-import crashes; reduces template depth for functions with many parameters.
ABI-breaking release drops Python 3.7, PyPy 3.8/3.9, and CMake <3.15; adds smart_holder, multi-phase init, and subinterpreter support.
- –Migrate to smart_holder with `py::class_<T, py::smart_holder>` for bidirectional unique_ptr/shared_ptr conversions and trampoline support.
- –Enable subinterpreter support by adding `py::multiple_interpreters::per_interpreter_gil()` as third parameter to PYBIND11_MODULE calls.
ABI bump (expected last before stable 3.0.0); fixes smart_holder support for const-only and shared_ptr<const T>, eliminates cross-DSO RTTI reliance on macOS.
- –Expect ABI breakage; recompile all extensions built against earlier 3.0.0 release candidates.
- –Update smart_holder code if using const-only smart pointers or shared_ptr<const T> in py::init().
Top in Developer Tools
Related Repositories
Discover similar tools and frameworks used by developers
nvm
nvm is a per-user, per-shell version manager for Node.js that allows switching between multiple installed Node versions from the command line. It works on any POSIX-compliant shell across Unix, macOS, and Windows WSL environments.
grafana
Query and visualize metrics from multiple data sources.
spdlog
Fast C++ logging with flexible sinks and fmt formatting.
speedtest
Measure bandwidth, latency, and packet loss via Cloudflare's edge network.
corepack
Enforces package manager versions specified in package.json.