Zig Language Toolchain (Python Wrapper)

0.15.2 · active · verified Tue Apr 14

The `ziglang` Python package redistributes the official Zig programming language toolchain, enabling Python projects to use Zig as a build dependency. It provides access to the Zig compiler and related utilities, including `zig cc` for C/C++ compilation, without requiring a separate system-wide Zig installation. The current version is 0.15.2, and new versions are released periodically to bundle updates to the Zig toolchain.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to invoke the bundled Zig toolchain using the `ziglang` Python package. It shows two primary methods: using Python's module execution (`python -m ziglang`) and directly calling the `python-zig` wrapper executable.

import sys
import subprocess

# Run the Zig compiler to print its version
result = subprocess.run([sys.executable, '-m', 'ziglang', 'version'], capture_output=True, text=True)
print(f"Zig version via python -m ziglang: {result.stdout.strip()}")

# Alternatively, use the 'python-zig' wrapper directly (if added to PATH or located)
# Note: This example assumes 'python-zig' is directly in PATH or located. 
# For robust use, you might need to find its path in the virtual environment's bin/Scripts directory.
try:
    result = subprocess.run(['python-zig', 'version'], capture_output=True, text=True, check=True)
    print(f"Zig version via python-zig: {result.stdout.strip()}")
except FileNotFoundError:
    print("Could not find 'python-zig' directly in PATH. Use 'python -m ziglang' or locate the executable.")
except subprocess.CalledProcessError as e:
    print(f"Error running python-zig: {e.stderr.strip()}")

view raw JSON →