18,028
stars
+2+1+4
2,333
forks
–
watchers
–
prs
–
issues
–
releases
–
discussions
README
.. figure:: https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png :alt: pybind11 logo pybind11 (v3) — Seamless interoperability between C++ and Python |Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |GitHub Discussions| |CI| |Build status| |SPEC 4 — Using and Creating Nightly Wheels| |Repology| |PyPI package| |Conda-forge| |Python Versions| Setuptools example _ • Scikit-build example _ • CMake example _ .. start pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code. Its goals and syntax are similar to the excellent Boost.Python _ library by David Abrahams: to minimize boilerplate code in traditional extension modules by inferring type information using compile-time introspection. The main issue with Boost.Python—and the reason for creating such a similar project—is Boost. Boost is an enormously large and complex suite of utility libraries that works with almost every C++ compiler in existence. This compatibility has its cost: arcane template tricks and workarounds are necessary to support the oldest and buggiest of compiler specimens. Now that C++11-compatible compilers are widely available, this heavy machinery has become an excessively large and unnecessary dependency. Think of this library as a tiny self-contained version of Boost.Python with everything stripped away that isn't relevant for binding generation. Without comments, the core header files only require ~4K lines of code and depend on Python (CPython 3.9+, PyPy, or GraalPy) and the C++ standard library. This compact implementation was possible thanks to some C++11 language features (specifically: tuples, lambda functions and variadic templates). Since its creation, this library has grown beyond Boost.Python in many ways, leading to dramatically simpler binding code in many common situations. Tutorial and reference documentation is provided at pybind11.readthedocs.io _. A PDF version of the manual is available here _. And the source code is always available at github.com/pybind/pybind11 _. Core features ------------- pybind11 can map the following core C++ features to Python: - Functions accepting and returning custom data structures per value, reference, or pointer - Instance methods and static methods - Overloaded functions - Instance attributes and static attributes - Arbitrary exception types - Enumerations - Callbacks - Iterators and ranges - Custom operators - Single and multiple inheritance - STL data structures - Smart pointers with reference counting like std::shared_ptr - Internal references with correct reference counting - C++ classes with virtual (and pure virtual) methods can be extended in Python - Integrated NumPy support (NumPy 2 requires pybind11 2.12+) Goodies ------- In addition to the core functionality, pybind11 provides some extra goodies: - CPython 3.9+, PyPy3 7.3.17+, and GraalPy 24.1+ are supported w