Skip to content

Repository files navigation

ZigX-Python

PyPI versionPyPI downloadsLicensePythonDocumentationCIGitHub issuesGitHub pull requestsGitHub 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.

Note: This Project is in Early Active Development, so there may be Breaking changes!

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-python.git
cd zigx
uv pip install -e .

Documentation

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

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.zigconststd=@import("std");
/// Add two integerspubexportfnadd(a: i32, b: i32) i32 {
returna+b;
}
/// Multiply two floatspubexportfnmultiply(a: f64, b: f64) f64 {
returna*b;
}
/// Calculate fibonacci numberpubexportfnfibonacci(n: u32) u64 {
if (n<=1) returnn;
vara: u64=0;
varb: u64=1;
vari: u32=2;
while (i<=n) : (i+=1) {
constc=a+b;
a=b;
b=c;
}
returnb;
}

Development Build

# Build and install in development mode
zigx develop

Use in Python

importmyproject# 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:

importthreadingimportmyprojectdefcompute():
# GIL is released during this call - other threads can runresult=myproject.heavy_computation(data)
returnresult# Run computations in parallelthreads= [threading.Thread(target=compute) for_inrange(4)]
fortinthreads:
t.start()
fortinthreads:
t.join()

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

constzigx=@import("zigx");
pubexportfnheavy_computation(data: [*]f64, len: usize) f64 {
// GIL is already released by ctypes// Do heavy work without blocking Pythonvarsum: f64=0;
for (0..len) |i| {
sum+=@sin(data[i]) *@cos(data[i]);
}
returnsum;
}

Type Mappings

Zig TypePython Typectypes Type
i8intc_int8
i16intc_int16
i32intc_int32
i64intc_int64
u8intc_uint8
u16intc_uint16
u32intc_uint32
u64intc_uint64
f32floatc_float
f64floatc_double
boolboolc_bool
voidNoneNone
[*]u8bytesc_char_p
usizeintc_size_t

Commands

CommandDescription
zigx new <name>Create a new ZigX project
zigx developBuild and install in development mode
zigx build --releaseBuild a release wheel
zigx publishBuild and publish to PyPI
zigx --helpShow 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-python

License

Apache License 2.0 - see LICENSE for details.

Contributing

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

Links

Releases

Packages

Used by

Contributors

Languages