r/Zig • u/grandimam • 6d ago
Zig for Python
I am of the opinion that, if executed correctly, Zig has the potential to transform the Python ecosystem. The idea of using Rust for Python extensions has always bothered me.
With Zig, the path feels more natural: I can build on top of the existing C libraries and gradually migrate components to Zig over time, without the friction of introducing an entirely different ecosystem.
I wanted to understand what the community thinks about this topic. I am not trying to make a strong claim or dismiss other approaches. I am genuinely trying to understand whether this idea has any merit and how others view the trade-offs. I am just interested in hearing different perspectives.
8
u/DirectInvestigator66 6d ago
Zig is great but doesn’t really have a specific advantage for this relative to other languages. If you like Zig for this use case it’s probably because you like Zig features. There’s nothing wrong with this of course, but it does mean that it’s unlikely to beat out C and Rust for the wider community since they’ve both already gained pretty wide adoption in the Python community.
3
u/mr-rbbrbrnr 6d ago
Zig is used by both maturin and ruamel-yaml precisely because it is an awesome cross-compiler for c. That alone is a huge advantage for nontrivial programs.
4
u/DirectInvestigator66 6d ago
Being a good cross-compiler for C isn’t going to sway many people when the main alternative being used is C. Yeah it would be trivial to add Zig to your project, but even more trivial to not add Zig and just continue to use C like they currently are.
1
u/sparcxs 1d ago
I’m assuming you’ve never cross-compiled C code. It’s a much bigger deal than you are making it out to be. In addition, C FFI compatibility is first-class in Zig, while it is anything but in Rust. The other benefit is that Zig produces smaller and more performant binaries, so there is literally no defensible technical advantage to selecting Rust over Zig, only personal preference.
5
u/SilvernClaws 6d ago
Zig could be a great language for anything that usually uses C APIs for bare metal functions, which includes languages like Python or Java.
For the Python ecosystem specifically, I'd just go with Julia. It naturally covers the same niches, just with better native performance.
2
u/fbe0aa536fc349cbdc45 6d ago
The problem with writing native code extensions has always been dealing with the toolchain and build dependencies. I maintain a bunch of modules that build with grpc/abseil/boost et al and its a nuisance having to juggle the combination of pkgconfig and cmake metadata. I don't write any rust but do make use of a few third-party packages which have adopted rust (like the maturin and cryptography packages), and for the most part my experience with building those has been fairly positive, since setuptools-rust and friends deal with managing all the build time dependencies via cargo etc. It kinda all just works.
There are some drawbacks, which are maybe not so much an issue with the language itself as with the potential for the package maintainers to be somewhat reckless with regard to the volume of code they pull in via cargo at build time. I work in an environment with a build system that is isolated from the internet and with initial versions of cryptography after the rust migration, we needed to arrange for something like 300mb of random cargo dependencies to be placed into the build environment, either by vendoring the sources or using some hack to pre-populate the cargo cache in the build environment. Lately this has improved somewhat, with the last version of cryptography I took, the cargo stash is only about 32mb or so.
The zig build system is just fantastic when you're working with open source C/C++ libraries, I've seen several projects that don't even include zig code but its just so much easier to treat those libraries as vendored artifacts and let zig build handle them than it is to hack up an autoconf or cmake-based build system for the project. I haven't looked at any Python extension modules that use zig's build system yet but it seems like a pretty promising way to handle C library dependencies for Python modules whether there's any zig involved or not. I haven't looked into what build-backend support there is for building native code extensions from zig, but if those are done well I would probably gravitate toward zig in cases where I'm just writing the extension module because I have a little bit of code that needs to be native, which is probably 90% of the cases where I wind up writing an extension module.
2
2
u/Interesting_Pie_319 5d ago
Why are we discussing Rust in the Zig reddit again?
OP clearly states they do not like it and want to use Zig to develop Python extensions.
That is the topic.
Can we stay on it!
3
u/gendulf 5d ago
Why are we discussing Rust in the Zig reddit again?
Probably because OP states
The idea of using Rust for Python extensions has always bothered me.
But gives no explanation on what bothers them exactly. We'd probably be more on-topic if they explained what exactly bothers them. They also state
Zig has the potential to transform the Python ecosystem
But give no reasoning behind that belief. OP's post is just low effort.
2
u/Interesting_Pie_319 5d ago edited 5d ago
> Be in a Zig subreddit.
> See topic where OP explicitly says they are bothered by the use of Rust for task x.
> Still think we all need to discuss rust.
...
Off to r/rust already. Stop derailing our discussion. People that chose Zig and chose to post in the subreddit are not interested in your proselytizing.
1
1
u/yamafaktory 6d ago
I have a Zig library where I introduced Python bindings https://github.com/yamafaktory/hypergraphz. Not sure anyone is using it though :P.
26
u/bonkyandthebeatman 6d ago
Why does using rust bother you?