pybind11
Seamless operability between C++11 and Python
A header-only C++ library that lets Python code call C++ functions and classes directly, so you can expose a fast C++ codebase to Python users without rewriting it.
pybind11 is a small library that connects C++ and Python so code written in C++ can be called naturally from Python, and to a lesser extent the other way around. Its main job is creating Python bindings for existing C++ code, a common need when a project's heavy lifting is in C++ for speed but the people using it would rather write Python. The README pitches it as a leaner alternative to the older Boost.Python library, stripped down to just what is needed for binding generation by leaning on modern C++11 features.
It is header-only: you do not link against a separate compiled library, you include its headers in your own C++ project. By using compile-time introspection, pybind11 infers type information so you write very little boilerplate to expose a C++ function or class to Python. It can map a wide range of C++ features, including value, reference, and pointer arguments, instance and static methods, overloaded functions, exceptions, enums, callbacks, iterators, custom operators, single and multiple inheritance, STL containers, smart pointers, and C++ classes whose virtual methods can be extended from Python. It also has integrated NumPy support, can vectorize functions over array arguments, and can expose internal storage through Python's buffer protocol for fast no-copy conversion between C++ matrix types and NumPy.
People reach for it when they have a C++ codebase they want to make usable from Python without rewriting it, or when building a Python module whose performance-critical parts live in C++. The README lists supported runtimes as CPython 3.8 and newer, PyPy3, and GraalPy. The project is BSD-licensed, created by Wenzel Jakob.
Where it fits
- Wrap an existing C++ library so Python users can import and call it like a native Python module.
- Build a Python extension whose performance-critical code runs in C++ without rewriting the whole project.
- Expose C++ matrix data to NumPy for fast zero-copy array operations between C++ and Python.