Skip to main content

magicbind

CI PyPI PyPI - Python Version License: MIT

The easy way to speed up Python bottlenecks with C++. No CMake, no build system, no boilerplate. Just point magicbind at your header and it takes care of the rest.

uv run magicbind add mylib.h

magicbind parses the header, generates nanobind glue code, compiles it, and installs the extension into your Python environment.

Install

uv add magicbind

magicbind uses your system compiler (g++, clang++, or MSVC) if one is available. If not, it falls back to a bundled Zig compiler via the ziglang package.

Basic usage

Given a header:

// math_utils.h
#include <vector>

inline double sum(const std::vector<double>& values) 
{
    double s = 0;
    for (auto x : values) s += x;
    return s;
}
uv run magicbind add math_utils.h

Then use it from Python:

import math_utils

math_utils.sum([1, 2, 3])  # 6.0

If the implementation is in a .cpp file instead, magicbind auto-detects it. You can also pass sources explicitly:

uv run magicbind add math_utils.h --source math_utils.cpp

System libraries (optional)

To use a library installed on your system, pass --pkg with its pkg-config name:

uv run magicbind add image_ops.h --pkg opencv4

On Linux and macOS you can use --pkg to resolve flags automatically via pkg-config. On Windows, pkg-config is not available; use --include, --lib, and --link to specify paths manually:

uv run magicbind add mylib.h \
  --include C:\mylib\include \
  --lib C:\mylib\lib \
  --link mylib

On Windows, magicbind automatically configures the MSVC build environment via vswhere.exe. Visual Studio or the standalone Build Tools must be installed (select the "Desktop development with C++" workload).

Rebuilding

When you change the header or source, run:

uv run magicbind build          # rebuilds all modules
uv run magicbind build mylib    # rebuilds one module

This replays the original add command with the same flags and compiler, without you having to remember them.

OpenCV

magicbind ships built-in type casters for common OpenCV types:

// image_ops.h
#include <opencv2/core.hpp>

cv::Mat blur(const cv::Mat& src, int kernel_size = 5);
cv::Size image_size(const cv::Mat& src);
import numpy as np
import image_ops

img = np.zeros((480, 640, 3), dtype=np.uint8)
blurred = image_ops.blur(img, 11)   # numpy array
w, h = image_ops.image_size(img)    # tuple

Supported types: cv::Mat ↔ numpy.ndarray, cv::Point / cv::Size / cv::Rect / cv::Scalar ↔ tuple, and their typed variants (cv::Point2f, cv::Rect2d, etc.).

Jupyter

Write C++ directly in a notebook cell:

%load_ext magicbind
%%magicbind math_utils
#include <vector>

double sum(const std::vector<double>& v)
{
    double s = 0;
    for (auto x : v) s += x;
    return s;
}
math_utils.sum([1.0, 2.0, 3.0])  # 6.0

The module is compiled and imported automatically. Re-running the cell recompiles and reloads. Requires magicbind in your environment.

Open In Colab

How it works

magicbind uses libclang to parse the header into an intermediate representation, generates a nanobind binding file, and compiles it with your system compiler (g++, clang++ or MSVC), falling back to a bundled Zig compiler if none is found. Build artifacts go into .magicbind/build/ and the compiled extension is installed directly into site-packages.

Templates

Template functions and classes are not bound directly. Expose concrete overloads in your header:

template <typename T>
T clamp(T value, T lo, T hi);

// Expose concrete overloads:
inline int    clamp(int v,    int lo,    int hi)    { return ::clamp(v, lo, hi); }
inline float  clamp(float v,  float lo,  float hi)  { return ::clamp(v, lo, hi); }
inline double clamp(double v, double lo, double hi) { return ::clamp(v, lo, hi); }

All three are available in Python as mylib.clamp. The right overload is picked automatically based on the argument types.

Metadata

Release files for magicbind 0.2.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magicbind 0.2.8
File Size Uploaded
magicbind-0.2.8.tar.gz 21.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for magicbind 0.2.8
File Interpreter ABI Platform
magicbind-0.2.8-py3-none-any.whl Python 3 none any Details

Total release size: 40.3 kB

Release files / magicbind-0.2.8.tar.gz

Download URL magicbind-0.2.8.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f33c9e85714c6b5fe30f86b6877e5fdedd5bdfd1ab2a83fc57d309230f5efc5a
BLAKE2b-256 checksum
How to use checksums
edf2e3bb1a62841943a1803fa3d1a3713d72ed33edea0559acd50f8fe72245d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / magicbind-0.2.8-py3-none-any.whl

Download URL magicbind-0.2.8-py3-none-any.whl
Size 18.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a9e93aae88ed58338a53e6e71a5b1626b855818790db2cc1395ab04e6882754
BLAKE2b-256 checksum
How to use checksums
b89e9c7cbbe0b02047241fd6dc4e8212cc87674030ea5039fc07dab42b3c51eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.8 This release

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page