nanobind 3.1.0 as C++23 modules: bind C++ to Python with no boilerplate headers to arrange.
English · 简体中文 · Upstream · Releases · mcpp · Index
This is a thin wrapper around nanobind
v3.1.0. The header API is exported unchanged: import mcpplibs.mwrap.nanobind; gives you nb::class_, nb::def, nb::cast, the
exception types and the object wrappers, while the STL, ndarray and operator
helpers live in submodules. The implementation comes from compat.nanobind
3.1.0; no upstream C++ source is vendored here.
The module interface and the runtime are compiled against the Python SDK on
<Python.h>'s include path. That must be the same Python ABI the bindings run
in — version, free-threaded configuration and Py_LIMITED_API. The manifest
declares xim:python 3.13.12 as the default; a consumer's own declaration, or
MCPP_PYTHON_INCLUDE_DIR, takes precedence (see Usage and API).
import mcpplibs.mwrap.nanobind;: handles, objects, classes, functions, casts, exceptions and utilities.import mcpplibs.mwrap.nanobind.stl;: casters forstd::string, containers,std::optional,std::variant,std::function, smart pointers, filesystem and chrono.import mcpplibs.mwrap.nanobind.ndarray;:nb::ndarray, DLPack types, device/order/framework tags andnb::dtype.import mcpplibs.mwrap.nanobind.operators;:nb::selfand operator-binding helpers.import mcpplibs.mwrap.nanobind.macros;:NB_VERSION_*asinline constexprconstants.
More Modules API -> Usage and API
NB_MODULE, NB_TRAMPOLINE, NB_OVERRIDE* and NB_MAKE_OPAQUE are macros and
cannot be exported by a named module. Include
<mwrap/nanobind/macros.hpp> before every import in that translation unit:
#include <mwrap/nanobind/macros.hpp> // first
import mcpplibs.mwrap.nanobind; // then
import mcpplibs.mwrap.nanobind.stl;A new project uses the default template:
mcpp new pybind --template mcpplibs.mwrap.nanobind@3.1.0
cd pybind
mcpp runThe generated program builds a Python module with NB_MODULE, registers it
with PyImport_AppendInittab, and imports it from the embedded interpreter.
In an existing project:
[dependencies]
mcpplibs.mwrap.nanobind = "3.1.0"
[xlings.workspace]
"xim:python" = "3.13.12"#include <mwrap/nanobind/macros.hpp>
import std;
import mcpplibs.mwrap.nanobind;
import mcpplibs.mwrap.nanobind.stl;
using namespace nanobind::literals;
NB_MODULE(example, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
}xim:python is the SDK used for both the module interface and the runtime.
If your application gets Python from uv, a virtual environment, or a custom
build, set MCPP_PYTHON_INCLUDE_DIR (and MCPP_PYTHON_HOME at run time for
an embedded interpreter). A system Python needs its development package.
| Feature | Default | Effect |
|---|---|---|
runtime |
yes | Compiles src/nb_runtime.cpp from compat.nanobind's forwarder, so one link of the module is enough. |
tests |
no | Links the Python embedding library into this package's own test executables only. |
To supply the runtime yourself (for example with NB_SHARED or a custom
Py_LIMITED_API build), disable defaults:
mcpplibs.mwrap.nanobind = { version = "3.1.0", default-features = false }Then compile <mcpp_nanobind_combined.hpp> in exactly one translation unit of
your program, as compat.nanobind documents.
Linux (GCC 16, LLVM 23), macOS (LLVM 23) and Windows (LLVM 23) are covered by
CI. Linux CPython extension modules must not link libpython; this package
never adds that link to a consumer. The embedded test executables and the
default template do link it, because they host an interpreter themselves.
The wrapper is Apache-2.0; see LICENSE and the attribution in NOTICE. Upstream nanobind remains BSD-3-Clause: its original LICENSE and README are retained byte for byte, with checksums in upstream/documents.json.