pybind11 & nanobind
Axel Huebl [LBNL], Wenzel Jakob [EPFL], Aaron Gokaslan [Cornell],�Ralf Grosse-Kunstleve [Google], Henry Schreiner [Princeton],�Ethan Steinberg [Stanford]
Zero-dependency pure C++ CPython/PyPy interface
Trivial to add to a project
No special build requirements
No pre-compile phase
Not a new language
Like a missing C++ API for CPython!
pybind11 2.10.1, 2.10.2, 2.10.3, 2.10.4 & 2.11
pybind11 2.11 (release today)
2.10.0 was released SciPy 2022,�2.11.0 is being released SciPy 2023!
Nanobind
Designed for Python 3.8+, C++17+, CMake 3.15+
Your bindings should conform to nanobind (vs. trying to support all of C++ in pybind11)
Focus on size • performance • compile time
Improvements:
Colocated C++/Python memory (2.3x smaller) • Less pointer chasing • Faster hash table • Vector calls • Not header only (has CMake helper) • Better lifetime management
New features:
0-copy N-dimensional arrays • CPython 3.12 Stable ABI support • Leak warnings • Low-level API
Minor new features:
Require returning existing Python instance • parameterized wrappers • Raw docstrings
Wenzel Jakob [EPFL]
Across the ecosystem! (nanobind_example)
[build-system]
requires = ["scikit-build-core", "nanobind"]
build-backend = "scikit_build_core.build"�
[project]
name = "nanobind-example"
version = "0.1.0"
[tool.scikit-build]
build-dir = "build/{wheel_tag}"
wheel.py-api = "cp312"
# Needed for full C++17 support
[tool.cibuildwheel.macos.environment]
MACOSX_DEPLOYMENT_TARGET = "10.14"
cmake_minimum_required(VERSION 3.15...3.26)
project(nanobind_example LANGUAGES CXX)
find_package(Python 3.8
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)
find_package(nanobind CONFIG REQUIRED)
nanobind_add_module(
nanobind_example_ext STABLE_ABI NB_STATIC
src/nanobind_example_ext.cpp
)
install(TARGETS nanobind_example_ext LIBRARY DESTINATION nanobind_example)
Requires nanobind 1.3.2+, scikit-build-core 0.4.3+, CPython 3.12.0b1+, cibuildwheel 2.13+, CMake 3.26.1+
On CPython 3.12+, this builds stable ABI wheels that support all future Python 3 releases with a single binary!
Plans
We are working on adopting some of the advancements from nanobind into pybind11.
New support for converting between arbitrary dimensional numpy arrays and Eigen::Tensors, which enables easier non-GIL processing of data from numpy
Dropping Python 3.6 next release.