Skip to main content

A maturin-like Python binding system for Zig - build Python extensions with Zig

Project description

ZigX

PyPI version PyPI downloads License Python Documentation CI GitHub issues GitHub pull requests GitHub last commit

A maturin-like Python binding system implemented in pure Zig.

ZigX makes it easy to create Python extensions using Zig, providing automatic ctypes-based bindings, type stub generation, GIL support, and cross-platform wheel building.

Features

  • 🚀 Pure Zig Implementation - No Python build dependencies beyond standard library
  • 📦 Bootstrapped Wheels - Pre-compiled binaries bundled for all platforms
  • 🔧 Development Mode - Hot-reload friendly develop command
  • 📝 Type Stubs - Automatic .pyi file generation for IDE support
  • 🔒 GIL-Safe - Automatic GIL release for ctypes calls (just like maturin/pyo3)
  • 🌐 Cross-Platform - Supports Linux (x86_64, aarch64), Windows (x86_64), and macOS (x86_64, arm64)
  • 🎯 Automatic Export Detection - No configuration needed, exports are detected from Zig source
  • uv Integration - Works seamlessly with modern Python tooling
  • 💪 No Zig Required - Install and use without needing Zig compiler (binaries pre-built)

Installation

From PyPI (Recommended)

pip install zigx

Or with uv (recommended):

uv pip install zigx

No Zig installation required! ZigX wheels include pre-compiled binaries for your platform.

From Source (Requires Zig 0.14.0+)

git clone https://github.com/muhammad-fiaz/zigx.git
cd zigx
uv pip install -e .

Documentation

📚 Full documentation is available at muhammad-fiaz.github.io/zigx

The documentation includes:

  • Getting started guide
  • API reference
  • Examples and tutorials
  • Troubleshooting guide

Create a New Project

zigx new myproject
cd myproject

This creates a minimal project structure:

myproject/
├── pyproject.toml       # Project configuration with zigx build backend
├── src/
│   └── lib.zig          # Your Zig code with exported functions
└── myproject/
    └── __init__.py      # Python package (bindings generated on build)

Write Zig Code

// src/lib.zig
const std = @import("std");

/// Add two integers
pub export fn add(a: i32, b: i32) i32 {
    return a + b;
}

/// Multiply two floats
pub export fn multiply(a: f64, b: f64) f64 {
    return a * b;
}

/// Calculate fibonacci number
pub export fn fibonacci(n: u32) u64 {
    if (n <= 1) return n;
    var a: u64 = 0;
    var b: u64 = 1;
    var i: u32 = 2;
    while (i <= n) : (i += 1) {
        const c = a + b;
        a = b;
        b = c;
    }
    return b;
}

Development Build

# Build and install in development mode
zigx develop

Use in Python

import myproject

# Call your Zig functions - GIL is automatically released!
result = myproject.add(1, 2)
print(f"1 + 2 = {result}")

# With type hints in your IDE!
product = myproject.multiply(3.14, 2.0)
fib_10 = myproject.fibonacci(10)

Release Build

# Build a release wheel
zigx build --release

This creates a wheel in dist/:

dist/myproject-0.1.0-cp314-cp314-win_amd64.whl

Publish to PyPI

# Build and upload
zigx publish

GIL Support

ZigX provides automatic GIL (Global Interpreter Lock) release just like maturin/pyo3. When you call a native function through ctypes, Python automatically releases the GIL for the duration of the call.

This means your Zig code can run in parallel with other Python threads without any extra configuration:

import threading
import myproject

def compute():
    # GIL is released during this call - other threads can run
    result = myproject.heavy_computation(data)
    return result

# Run computations in parallel
threads = [threading.Thread(target=compute) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()

For explicit GIL control in Zig, you can use the zigx helpers:

const zigx = @import("zigx");

pub export fn heavy_computation(data: [*]f64, len: usize) f64 {
    // GIL is already released by ctypes
    // Do heavy work without blocking Python
    var sum: f64 = 0;
    for (0..len) |i| {
        sum += @sin(data[i]) * @cos(data[i]);
    }
    return sum;
}

Type Mappings

Zig Type Python Type ctypes Type
i8 int c_int8
i16 int c_int16
i32 int c_int32
i64 int c_int64
u8 int c_uint8
u16 int c_uint16
u32 int c_uint32
u64 int c_uint64
f32 float c_float
f64 float c_double
bool bool c_bool
void None None
[*]u8 bytes c_char_p
usize int c_size_t

Commands

Command Description
zigx new <name> Create a new ZigX project
zigx develop Build and install in development mode
zigx build --release Build a release wheel
zigx publish Build and publish to PyPI
zigx --help Show help information

Requirements

System Requirements

  • Operating System: Linux, macOS, or Windows
  • Architecture: x86_64, ARM64

Software Requirements

  • Zig Compiler: 0.14.0 or later (0.15.0+ recommended)
  • Python: 3.8 or later
  • Build Tool: uv (recommended) or pip or hatchling or poetry

Optional Dependencies

For development:

  • pytest (testing)
  • ruff (linting)
  • mypy (type checking)
  • mkdocs (documentation)
  • hatchling (build backend)

For documentation building:

  • mkdocs-material
  • mkdocstrings[python]

Documentation

Full documentation is available at muhammad-fiaz.github.io/zigx

License

Apache License 2.0 - see LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zigx-0.0.1.tar.gz (152.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zigx-0.0.1-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file zigx-0.0.1.tar.gz.

File metadata

  • Download URL: zigx-0.0.1.tar.gz
  • Upload date:
  • Size: 152.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"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}

File hashes

Hashes for zigx-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1c282584156150f45e96306e4527adf92f0510dec4554be77d7ea7618b07d9ab
MD5 6e12cc4795e9ab33be3d5a75ae09cdf8
BLAKE2b-256 3e53c1bfd586e33fbd46b3ddc394a5a1ec67bb1370507fd530a9dc5cb6ba2c89

See more details on using hashes here.

File details

Details for the file zigx-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: zigx-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"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}

File hashes

Hashes for zigx-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea2df6813f72024f58d714382818345a82fc5a1eafa7d06b957fac38679c88bd
MD5 c3cd299f25c37ac7cd55133117f0afaa
BLAKE2b-256 19b11737efc5c47343c9f9f085e4dc1f2058f3fce7899b0b5724072db29866e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page