muhammad-fiaz/zigx
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!
develop command.pyi file generation for IDE supportpip install zigx
Or with uv (recommended):
uv pip install zigx
No Zig installation required! ZigX wheels include pre-compiled binaries for your platform.
git clone https://github.com/muhammad-fiaz/zigx.git
cd zigx
uv pip install -e .
📚 Full documentation is available at muhammad-fiaz.github.io/zigx
The documentation includes:
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)
// 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;
}
# Build and install in development mode
zigx develop
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)
# Build a release wheel
zigx build --release
This creates a wheel in dist/:
dist/myproject-0.1.0-cp314-cp314-win_amd64.whl
# Build and upload
zigx publish
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;
}
| 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 |
| 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 |
For development:
For documentation building:
Full documentation is available at muhammad-fiaz.github.io/zigx
Apache License 2.0 - see LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.