explaingit

pybind/pybind11

17,853C++Audience · developerComplexity · 3/5LicenseSetup · moderate

TLDR

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.

Mindmap

mindmap
  root((pybind11))
    What it does
      C++ to Python bridge
      Header-only library
      NumPy support
    Tech stack
      C++11
      Python 3.8+
      CPython and PyPy
    Use cases
      Wrap C++ libraries
      Fast Python extensions
      Zero-copy arrays
    Features
      Virtual method override
      STL container mapping
      Smart pointer support
    Audience
      C++ developers
      Python library authors
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Wrap an existing C++ library so Python users can import and call it like a native Python module.

USE CASE 2

Build a Python extension whose performance-critical code runs in C++ without rewriting the whole project.

USE CASE 3

Expose C++ matrix data to NumPy for fast zero-copy array operations between C++ and Python.

Tech stack

C++PythonNumPyCPythonPyPyCMake

Getting it running

Difficulty · moderate Time to first run · 30min

Requires a C++11 compiler and CMake or setuptools to build the extension module before it can be imported in Python.

Use freely for any purpose including commercial use, as long as you keep the copyright notice (BSD license).

In plain English

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.

Copy-paste prompts

Prompt 1
Show me a minimal pybind11 C++ file that exposes a function taking a NumPy array and returning a modified array, plus the CMakeLists.txt to build it.
Prompt 2
Write pybind11 bindings for a C++ class with a constructor, instance methods, and a read-only property.
Prompt 3
How do I propagate a C++ exception through pybind11 so Python callers see a proper Python exception?
Prompt 4
Show me pybind11 bindings for a C++ class hierarchy where Python subclasses can override virtual methods.
Prompt 5
Write a setup.py using pybind11 to build a Python extension module so it installs with pip.
Open on GitHub → Explain another repo

← pybind on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.