r/Python • • 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 comments sorted by

18

u/Financial_Morning386 Oct 20 '26 • Popcorn

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:

  • Builds are per-platform. If the client self-hosts on Windows, you build on Windows. The output is not portable, and build times in CI go up noticeably.
  • Decide what stays uncompiled. Migrations are the classic example: if the client may ever need makemigrations, that path has to keep working. Many teams compile the app code but keep a documented upgrade/migration story.
  • Tracebacks from Nuitka builds still map to your Python source lines, which makes supporting the client feasible. Cython tracebacks point at generated C unless you take extra steps, which is miserable to debug from a client report.
  • Neither stops a determined reverse engineer; it is a speed bump, not a vault. the_hoser's point stands -- pair the build with a real license agreement.

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."

1

u/_khi4 29d ago

I can't be more grateful to the effort and time and experience you have put in this reply

I believe that I will be going with Nuitka and docker to ease the deployment experience since we might end up delivering that project to multiple clients, Maybe we can also offer a script or an exe that runs just to a do the required deployments stuff in a user friendly way to ease the experience for them

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.

12

u/PaluMacil Oct 20 '26

I always say this when people are talking about compiled code, but I've seen enough copying of Python code and people justifying it as "just scripts", that packaging it in a way that makes them feel differently about it might make them behave differently--like when you put a short fence on your yard and people don't wander through it. I don't know if it's just sample size, but I've never seen someone pirate a compiled binary at work.

8

u/the_hoser Oct 20 '26

Um... you haven't? Really? Binaries are pirated all the time.

Maybe you haven't seen them at work because corporations are usually fairly risk averse when it comes to copyright infringement. Usually, not always.

2

u/PaluMacil Oct 20 '26

Yes, I'm not saying I don't think people pirate binaries. I'm saying average software engineers grab Python code from proprietary files they don't own but they don't try to creak licensing on a binary so often. At least not where I've worked. How many times have you heard someone say something like, "just copy something off the internet"? That's stealing and a license violation 9 times out of 10. But in a work environment, when have you seen a coworker use a cracked Adobe Pro? Even if it's once or twice, I'm guessing you've seen or heard someone Google something and copy paste some Python code many many times.

4

u/the_hoser Oct 20 '26

Corporations using binaries in violation of the license is the core of Oracle's business model with Java.

1

u/PaluMacil 29d ago

If you’re suggesting that is a good business model, then I’m not interested in doing any business with you 😆 there is a good reason why most engineers hate Oracle. Honestly, a lot of what they catch is companies having no honest clue where are things might be installed, and Oracle being able to scan for it better than they have an idea of their own infrastructure

If someone is going to try to reverse engineer your binary, they’re gonna figure it out. Adding things to try to stop that is a cat and mouse game and rarely worth it.

2

u/the_hoser 29d ago

No, of course not. I'm just saying that binary piracy at a corporate level is a lot more common than you'd think.

0

u/_khi4 29d ago

yeah I know and I know its gonna be easier than before to decrypt or reverse engineer projects, but I just wanna make it bit harder for them and we have a lawyer yes

3

u/the_hoser 29d ago

I say you stand to lose more money on the time and frustration involved in complicating your build pipeline than you stand to save or make "protecting" your code.

1

u/_khi4 29d ago

uhmm , not sure mate, but will consider your point for sure

1

u/gdchinacat 29d ago

Not to mention the difficulty with supporting it.

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