Zig build-lib not finding additional sources in current directory

setuptools-zig has been using @cImport for many years to include the Python.h file. Since this is to-be-deprecated, I used zig translate-c to create a python.zig file as a first step.

I changed the import in my test program sum.zig to:

const c = @import("python");

and called the usual zig build-lib with the appropriate parameters (leaving out the -I /path/to/Python.h. Doing so I get the error:

sum.zig:9:19: error: no module named 'python' available within module 'sum'
const c = @import("python");
                  ^~~~~~~~
referenced by:
    PyInit_zig_sum: sum.zig:120:36
    root: /opt/zig/zig-aarch64-macos-0.16.0/lib/std/start.zig:13:22
    3 reference(s) hidden; use '-freference-trace=5' to see all references

I looked at zig build-lib --help and experimented with the -M option to create a python module from python.zig but could not solve this issue. -M is not mentioned in the Zig Language Reference at all, and although there are examples with zig build-lib and zig build-exe in there, I could find none that import anything but "std" (except for the cImport examples).

I haven’t tried to write the translated python.zig file to the standard library directory, to see if that works (and verify if the translation did not introduce errors), but that is not a solution for someone using setuptools.zig as they may not have write access in the Zig installation directory.

I also don’t necessarily want to write a build.zig (for which I know how to handle multiple .zig files) from the Python extension build sytem because of the increased complexity this introduces (and the increase in compile time).

FWIW this is the full invocation I used:

/opt/zig/0.16.0/zig translate-c -D PY_SSIZE_T_CLEAN /opt/python/3.14/include/python3.14/Python.h > python.zig
/opt/zig/0.16/zig  build-lib -dynamic -fallow-shlib-undefined -femit-bin=zig_sum_tr.cpython-314-darwin.so -lc -L /opt/python/3.14/lib -O ReleaseFast sum.zig

sum.zig is as in the zig code only example in setuptools.zig README with the cImport changed as desribed.

What are the options I need to add to zig build-lib to get this to work?

Try @import("python.zig"). If you want to import a file, that’s the way to go.

1 Like

I could have sworn that I tried that, but probably I didn’t (or made some other breaking changes at the same time).

1 Like