Make executable file from multiple pyx files using cython

People are tempted to do this because it’s fairly easy to do for the simplest case (one module, no dependencies). @ead’s answer is good but honestly pretty fiddly and it is handling the next simplest case (two modules that you have complete control of, no dependencies).

In general a Python program will depend on a range of external modules. Python comes with a large standard library which most programs use to an extent. There’s a wide range of third party libraries for maths, GUIs, web frameworks. Even tracing those dependencies through the libraries and working out what you need to build is complicated, and tools such as PyInstaller attempt it but aren’t 100% reliable.

When you’re compiling all these Python modules you’re likely to come across a few Cython incompatibilities/bugs. It’s generally pretty good, but struggles with features like introspection, so it’s unlikely a large project will compile cleanly and entirely.

On top of that many of those modules are compiled modules written either in C, or using tools such as SWIG, F2Py, Cython, boost-python, etc.. These compiled modules may have their own unique idiosyncrasies that make them difficult to link together into one large blob.

In summary, it may be possible, but for non-trivial programs it is not a good idea however appealing it seems. Tools like PyInstaller and Py2Exe that use a much simpler approach (bundle everything into a giant zip file) are much more suitable for this task (and even then they struggle to be really robust).


Note this answer is posted with the intention of making this question a canonical duplicate for this problem. While an answer showing how it might be done is useful, “don’t do this” is probably the best solution for the vast majority of people.

Leave a Comment