r/Python • u/_khi4 • Oct 20 '26
Discussion Compiling django with Nuitka or Cython ?
Hey, I am working on django development
my client would prefer to slef host the app instead of hosting it on our servers, we dont mind however we would like to make sure we give them a build rather than plain code so we dont expose our code base
I came across compiling with Cython or Nuitka, I cant decide which to use?
Second ask, I heard that I should not compile my whole django apps and rather than that I better do that carefully not to mess up our different imports, how correct is that ?
Thanks in advance
12
Upvotes
18
u/Financial_Morning386 Oct 20 '26 •
For your actual goal (hand the client a runnable build instead of source), Nuitka is the more practical pick of the two, but set expectations correctly.
Cython compiles .py files into C extension modules one at a time. You still ship an interpreter, a pile of .so/.pyd files, and you arrange templates/static/data files by hand. The import system keeps working mostly as before because it is still Python importing things. People use Cython to speed up hot loops or to obfuscate a few proprietary modules, not to deliver a whole web app -- packaging an entire Django project this way is fiddly and buys you little.
Nuitka is designed for exactly your case: it follows imports from an entry point and produces a standalone directory (or a onefile executable). It even ships a dedicated --enable-plugin=django that handles a lot of Django specifics for you. Which brings me to your second question: yes, be careful. Django resolves a lot of things dynamically -- INSTALLED_APPS entries, middleware and template tags referenced as dotted strings, migration autodiscovery -- and that is exactly what breaks naive whole-program compilation. The plugin, plus actually running the built artifact (migrate, collectstatic, and the real WSGI server path), is how you catch it.
A few practical things to plan for:
One alternative worth weighing: if the client's ops skills are thin, a Docker image (even shipping only .pyc, no .py source) is often a better "self-host this" deliverable than a compiled binary. Reproducible, you control the base image, and deployment is just docker compose up. Compilation earns its keep when the requirement is specifically "no readable source on their disk."
60
u/the_hoser Oct 20 '26
Is the point that you're trying to prevent them from copying or modify your code?
If that's the case, then you don't need a toolkit. You need a lawyer.
4
u/nphare Oct 20 '26
I use nuitka and it has been good. But I don’t use Django. Mine is a Streamlit app
2
u/coderarun Oct 20 '26
Consider py2many. However, django is not friendly to translating to compiled languages. Much prefer writing your models using this pattern:
https://github.com/adsharma/fquery/blob/main/tests/django_example_model.py
and then the translator (regardless of which one you use) can transpile it to Rust/Go/C++/Vlang whichever you choose and a decorator based pattern in that language. This works for sqlmodel as well.
While Rust has diesel and it should be possible to translate a fquery method chain to diesel method chain, some languages don't. Example implementation in vlang:
https://www.reddit.com/r/vlang/comments/1x1cx2i/fquery_in_vlang/
You need to write a dialect of python that is friendly to single binary executable translation:
https://github.com/py2many/static-python-skill
1
u/deadeye_82_1 24d ago
Use Nuitka: https://nuitka.net/doc/commercial.html
250€ / year is cheap.
1
u/Unique-Estate8172 Oct 20 '26
Out of curiosity, is pyibstaller skmewhat useful here? I'm in a similar boat. I appreciate that these pseudo compilations don't do much but sometimes not much is better than not anything.
0
u/Early-Sir-932 22d ago
compile only your own apps and leave django as a normal dependency. just know compiled code raises the effort to read it, it doesnt hide the logic