Example on how to build and test a Python extension written in Zig.
This example uses translate-c/@cImport
and zig-python
build plugin to automatically link Python library and generate bindings.
Tested with Zig version 0.14.0
. CI is run for Linux, Windows and macOS.
You can simply install Python extension as a regular shared library:
$ zig build
However, you won't be able to run test.py
straight after that.
CPython expects certain filename format which is not the same
as Zig defaults for shared libraries:
module.so
on Linux systems (instead oflibmodule.so
)- same on MacOS (instead of
.dylib
) module.pyd
on Windows (instead of.dll
)
You can copy them to your current dir or PYTHONPATH in a similar
maner (adjust for your OS) and then run python test.py
:
$ cp zig-out/lib/libmodule_name.so ./module_name.so
$ python test.py
// Or:
$ cp zig-out/lib/libmodule_name.so zig-out/lib/module_name.so
$ PYTHONPATH="zig-out/lib/" python test.py
To simplify this, build.zig
has test step defined for you:
$ zig build test
This project is REUSE-compliant, text of licenses can be found in LICENSES directory. Short overview:
- Code is licensed under 0BSD.
- This README and CI files are licensed under CC0-1.0.