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, macOS 11 or newer, or Windows 10 or newer.
- CPython 3.13 or newer, including free-threaded builds.
- Zig 0.17. Python build tools install it through the
ziglangpackage 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.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zypy-0.3.0.tar.gz | 31.0 kB | Without provenance | Details for zypy-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zypy-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for zypy-0.3.0-py3-none-any.whl |
Total release size: 69.7 kB
Release files / zypy-0.3.0.tar.gz
| Download URL | zypy-0.3.0.tar.gz |
|---|---|
| Size | 31.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c16d670b7e3ecac31ba1623ad90383697c14ca99283103b6c9d8bcbd8acf0fc
|
|
BLAKE2b-256 checksum How to use checksums |
1357510c1754a0da5df9bc014ecf9a00974b7de1659a67ba004d48a3ddcfcb4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.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 / zypy-0.3.0-py3-none-any.whl
| Download URL | zypy-0.3.0-py3-none-any.whl |
|---|---|
| Size | 38.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2489a64331bf8bcc1cfa47072ae0cec6cc9b5dd3d0da65e0c0352f6067c163b8
|
|
BLAKE2b-256 checksum How to use checksums |
9c0f1a4b939d606f15b0c665609d2038670a31ca400196bb1d42420f4718f9fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.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}
|
Security for zypy 0.3.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.