nanobind: tiny and efficient C++/Python bindings

2.12.0 · active · verified Thu Apr 09

nanobind is a C++17-first binding library designed for creating efficient and minimalistic C++/Python interoperability layers. It enables seamless exposure of C++ types and functions to Python, often with significantly faster compile times, smaller binaries, and lower runtime overhead compared to alternatives like pybind11 and Cython. The library is actively developed, with a focus on performance and modern C++ practices, and supports Python 3.9+ and PyPy 7.3.10+.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the Python side of interacting with a nanobind-generated extension. It assumes a C++ module (e.g., `_my_ext_impl`) has been built and exposed via a Python package (`my_ext`), following the recommended packaging guidelines. The C++ code defines a simple 'hello' function, which is then imported and called from Python.

# Assuming you have built a nanobind extension named 'my_ext'
# For example, with C++ code like:
# NB_MODULE(my_ext_impl, m) { m.def("hello", []() { return "Hello world!"; }); }
# And an __init__.py with: from ._my_ext_impl import hello

import my_ext

message = my_ext.hello()
print(message)

view raw JSON →