Skip to main content

ZyPy logo

ZyPy

ZyPy lets you write Python extensions in Zig and call Python from Zig programs. It includes a Zig library, a Python build backend, and the zypy command-line tool.

You can export functions and classes, work with Python objects and buffers, or embed Python in a Zig program. ZyPy builds Python packages and generates type stubs so editors and type checkers can understand your extensions.

ZyPy is experimental and may be unstable in some scenarios.

Requirements

  • Linux with glibc or musl, or macOS 11 or newer.
  • CPython 3.13 or newer, including free-threaded builds.
  • Zig 0.17. Python build tools install it through the ziglang package if needed.

The steps below use uv to manage Python environments and builds.

Quick start

ZyPy is on PyPI as zypy. Create a project and call it from Python:

uvx zypy new hello
cd hello
uv sync
uv run python -c "import hello; print(hello.add(1, 2))"

This creates a project, builds its extension, and calls it from Python. The result is 3.

The generated src/root.zig defines a Python module with one function:

const std = @import("std");
const py = @import("zypy");

fn add(a: i64, b: i64) !i64 {
    return std.math.add(i64, a, b);
}

pub const module = py.module(.{
    .doc = "hello, written in Zig.",
    .functions = .{
        .add = py.function(add, .{ .doc = "Add two numbers." }),
    },
});

comptime {
    py.exportModule(module);
}

ZyPy converts arguments and return values for you. Returning a Zig error raises a Python exception. For example, if this sum is too large for i64, Python receives OverflowError.

After changing Zig code, run your code again with uv run, which rebuilds the extension when Zig files change. Run uv build to create a wheel and a source distribution for sharing or installation.

Types

Zig functions can take and return these types. ZyPy converts them to and from Python values.

Zig Python
i8, u32, i128, ... (any width) int, range-checked
f32, f64 float
bool bool; arguments accept any object, by truthiness
void (return) None
?T T or None
E!T (return) T, or an exception (py.raise(...), or RuntimeError for other Zig errors)
tuples, e.g. struct { i64, py.Str } tuple
other structs dict; arguments accept any mapping, with one key per field
enums str with the tag name; arguments also accept enum.Enum members
[]const u8, py.Bytes bytes
string literals (return) str
other slices and arrays (return) list
py.Object any object
py.Str str
py.List, py.Tuple list, tuple of any length
py.Dict dict
py.Set, py.FrozenSet set, frozenset
py.Sequence(T) any iterable except str
py.Iterator(T) any iterable except str, read one item at a time
py.Buffer(T), py.BufferMut(T) bytearray, memoryview, array.array, NumPy arrays, ...
py.StridedBuffer(T), ... buffers that aren't contiguous, such as the NumPy slice a[::2]
py.Exception any exception object
classes: *T, *const T, T instances of a Zig struct that declares pub const class = py.class(...)
py.Instance(T) an instance of class T
py.Class(T) class T or a subclass; as a method's first parameter, makes a class method

Documentation and examples

ZyPy documentation and API reference is available at zypy.jokelbaf.dev.

The repository also includes three example projects:

  • hello: a Python extension written in Zig.
  • mixed: a Python package with a Zig extension.
  • embed: a Zig program that runs Python.

License

ZyPy is licensed under the MIT License.

Metadata

Release files for zypy 0.1.0

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

Source distribution (sdist)

Source distribution for zypy 0.1.0
File Size Uploaded Provenance Actions
zypy-0.1.0.tar.gz 29.6 kB Without provenance Details for zypy-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 67.0 kB

Release files / zypy-0.1.0.tar.gz

Download URL zypy-0.1.0.tar.gz
Size 29.6 kB
Tags Source
SHA-256 checksum
How to use checksums
257657fd2edba64da08b827dc669cfa90edfd135cd357aa9e614927680f23c9c
BLAKE2b-256 checksum
How to use checksums
bf1fbdc5309d8d80e903fec1e01d50745b04ef2cce5a717a12fbe45de30d6356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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 / zypy-0.1.0-py3-none-any.whl

Download URL zypy-0.1.0-py3-none-any.whl
Size 37.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c723a133d86cf27ab31c905800959369e0fb84c4c0d293f94a31329103723208
BLAKE2b-256 checksum
How to use checksums
2f00d628faa57267b884eee88420af5f97985ffe84a3e1a46d99c6f79aed5e23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.4 {"installer":{"name":"uv","version":"0.13.4","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}

Security for zypy 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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