Py2Native Python to native compilerPython-zu-Native-Compiler

← All posts

2026-11-21

Why Compiled Python Is Harder to Reverse Engineer

Why Compiled Python Is Harder to Reverse Engineer
python compiler cython code protection open source

Why Compiled Python Is Harder to Reverse Engineer

If you hand someone your Python application, the first question worth asking is embarrassingly simple: how much of my logic can they read?

For a .py file, the answer is all of it. Not “most of it after some work” — all of it, immediately, in a text editor, with your comments intact. That is the starting point for every conversation about protecting Python source code, and it’s the question this article answers: what changes when the artifact you ship is no longer text?

Spoiler: compilation doesn’t make your code unreadable in some absolute sense. It changes who has to do how much work to recover your logic — and that shift is usually the whole ballgame. Here’s the honest version of why it works.

The Plain-Language Answer: What Actually Ships in a .py File

Start with a piece of ordinary business logic — say, a pricing engine:

# pricing.py
DISCOUNT_THRESHOLD = 12_500  # EUR, negotiated with sales in Q3
TIER_MULTIPLIER = {"standard": 1.0, "partner": 0.82, "oem": 0.61}

def quote(units: int, unit_price: float, tier: str) -> float:
    """Return the net quote. Partner tiers below threshold get no volume break."""
    gross = units * unit_price
    if gross >= DISCOUNT_THRESHOLD and tier != "oem":
        gross *= 0.93  # volume break, not documented externally
    return round(gross * TIER_MULTIPLIER[tier], 2)

Ship that file and you have shipped:

  • Readable source — the logic in plain text, line by line.
  • Meaningful names — DISCOUNT_THRESHOLD, TIER_MULTIPLIER, quote. These are a map of your domain model.
  • Comments and docstrings — the ones above literally explain your pricing policy and its history.
  • Structure — module boundaries, function decomposition, the shape of your design decisions.

An attacker — or a competitor, or a customer who wants to remove a tier restriction — doesn’t need to reverse engineer anything. They open the file.

A compiled artifact is different in kind. What ships is machine code plus a runtime: the same behaviour, expressed as instructions for a CPU rather than as text for a human. The names are gone. The comments never existed. The original file boundaries are no longer present as text.

The rest of this article is about how much harder that makes recovery — and where the limits are. Because the correct framing is: compilation raises the cost of reverse engineering. It does not make it mathematically impossible, and any vendor who tells you otherwise is selling you something.

A Real-World Analogy: Recipe vs. Finished Dish

Source code is a recipe. Anyone who gets the recipe can reproduce the dish, scale it, tweak the salt, and serve it under their own name.

A compiled binary is the finished dish. You can taste it. You can form hypotheses about what’s in it. You can, with effort and equipment, work out a lot — but you can’t trivially reproduce it, and you certainly can’t hand it to a line cook and say “make this.”

Now extend the analogy one step further, because that’s where most Python developers get caught out. Bytecode is a recipe written in shorthand. Shipping .pyc files instead of .py files feels like protection, but the shorthand is standardized and the decoder is widely available. It’s obfuscation with a published key.

So there’s a spectrum of effort required to recover intent:

What you ship Effort to recover your logic
.py source None — open in an editor
.pyc bytecode Low — decompilers reconstruct near-source
Compiled native code High — disassembly, manual control-flow reconstruction

The honest limit of the analogy: a determined chef with a lab can reverse-engineer a dish. It just costs far more than asking for the recipe. For most proprietary software, that economic gap is exactly what you’re buying.

Under the Hood: Why Native Code Is Harder to Read

Compilation translates Python down through several layers. In the Py2Native pipeline, your .py files become Cython-generated C, the C is compiled to object files with a platform toolchain, and the objects are linked into a native artifact (a shared library, or an executable).

By the time an attacker holds that artifact, the following are simply not present:

  • Variable, function, and module names as they appeared in your source
  • Comments, docstrings, and type annotations
  • The original file and directory layout
  • High-level control structures as you wrote them

What’s left is disassembly, symbol tables (often stripped or reduced to mangled names), imports against the CPython runtime, and a control graph that has to be reconstructed by hand. A competent reverse engineer can still get somewhere — but they’re now doing weeks of work to recover something that previously took ten seconds of cat.

Contrast that with bytecode. Python bytecode is designed to be executed by an interpreter, and it retains a great deal of structure. Tools exist that turn it back into something close to your original source. That’s why .pyc-only “protection” is a speed bump, not a wall.

I’ve kept this section deliberately shallow; if you want the mechanics of the full pipeline, the deeper explainer is Compile Python to a Native Binary Without Writing a Single .pyx File.

The Zero-Config Way: Plain Python In, Native Binary Out

Here’s where most Python developers get stuck on the “compile it” advice: the traditional path is Cython with hand-written .pyx files, .pxd declarations, and a manual cythonize step — or worse, C extensions. That’s a real engineering project, and it means maintaining two versions of your codebase forever.

Py2Native takes the other path. You write plain Python. That’s it — no Cython syntax, no .pyx/.pxd files to maintain for your application code, no manual transpilation step, no C extensions. Cython runs under the hood and stays there.

The workflow is a single command:

uv run py2native build main.py *.py --wheel dist

That globs your sources under the base directory, compiles them through Cython to C, compiles the C with your platform toolchain (MSVC on Windows, GCC on Linux, Clang on macOS), and produces a PEP 427 wheel containing the compiled .pyd/.so.

If you’d rather ship a uv-managed deployment directory than a wheel:

uv run py2native build main.py *.py --embed build/app

Or build a shared library instead of an executable:

uv run py2native build main.py *.py --library

In library mode the wheel contains one compiled shared library holding all your modules, plus two generated files that are both required: __init__.py, a meta-path finder that redirects import mypkg.submodule into the native library, and __main__.py, which makes python -m mypkg work. You don’t write either of them.

Two things make this practical rather than theoretical:

  • Third-party libraries stay unmodified. You don’t have to compile your dependency tree, annotate it, or write build config for it. Libraries keep working as-is, with minimal or no configuration — which is the main reason “just compile everything” projects stall.
  • The core compiler is MIT licensed. You can read it, fork it, and ship it in commercial software. The Pro plugin is the commercial add-on, and it’s where license verification lives.

Practical requirements: CPython 3.11–3.15 (including free-threaded 3.14t and 3.15t), a platform C compiler, Windows 8+/manylinux2014 or musl/macOS, x86-64 or ARM64, and internet access during the build for downloading Python distributions and libraries. On Linux, auditwheel repair is part of the pipeline.

Adding License Verification Without Shipping the Check

Compiling protects your logic. It doesn’t, by itself, stop someone from running your binary on a machine you didn’t authorize. That’s a separate problem, and it’s where the Pro plugin comes in.

The Pro plugin adds three commands and a build flag:

uv run py2native keygen private.pem public.pem
uv run py2native sign --private private.pem payload.json license.dat
uv run py2native show --public public.pem license.dat
uv run py2native build main.py *.py --license license.dat --public public.pem

keygen generates an EC P-256 keypair. sign produces a JWT signed with the private key. show displays the claims, optionally verifying the signature against the public key.

The important part is what ends up in the executable: only the public key, plus the signature-verification logic compiled into native code. The elliptic-curve verification is handled by compiled code inside the product — no third-party crypto library shipped alongside it for someone to hook.

To wire that into your app, you declare the verification entry point in a .pxd file that ships with the Pro plugin, so the call is resolved at compile time:

# _license.pxd
from _p2n_bootstrap cimport _runtime_verify_es256_jwt

cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)

And call it with your license string and the expected claims:

# _license.pyx
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")

That call sits in a small compiled bridge module; the rest of your application stays plain Python and imports the bridge like any other module. The plugin bakes the signature-verification code and the public key into the executable, so the check travels with the binary and can’t be swapped out for a stub without invalidating the build.

If you want the end-to-end walkthrough of that flow, see Verify a Signed License Key in a Compiled Python App.

Why It Matters for Shipping Proprietary Software

Three scenarios where this stops being an abstract security discussion:

  • Internal tooling distributed to contractors or partner organizations, where you want the behaviour but not the implementation.
  • Licensed desktop applications, where the binary is installed in an environment you don’t control.
  • Code embedded in customer infrastructure — on-prem deployments, appliances, edge boxes — where “just don’t share the source” isn’t enforceable.

There’s a distinction worth keeping straight here: protecting your own code is not the same as complying with third-party licenses. Py2Native compiles your modules. Third-party libraries remain as Python source, unmodified, so LGPL libraries remain replaceable by the end user — which is what compliance requires. Don’t conflate the two in your release notes.

And the honest constraint: because third-party libraries stay as Python source, monkey-patching is still possible. Someone can patch a dependency at runtime. What they can’t easily do is read or modify your compiled business logic. That’s the trade — and it’s the right trade for most commercial software.

For deployment-specific guidance, the Docker-focused walkthrough is here: Protect Python Code in a Docker Build with a Single Command.

Common Misconceptions About Compiled Python

Myth: compiling makes code uncrackable. Reality: nothing makes code uncrackable. Compilation raises cost and effort — removal of names, comments, and structure, plus the conversion to machine code — and that’s usually enough to protect business logic from casual and mid-tier attempts. Plan for “expensive,” not “impossible.”

Myth: you have to rewrite your code in Cython or C. Reality: with Py2Native you write plain Python and run uv run py2native build main.py *.py --wheel dist. Cython is an implementation detail. Hand-written .pyx and C extensions are the hard way — and for application code, you don’t need to go there.

Myth: third-party libraries break. Reality: they’re left unmodified and keep working as-is, with minimal or no configuration. That’s the difference between a tool that fits your project and a tool that becomes your project.

Myth: you need a separate licensing system bolted on. Reality: the Pro plugin bakes ES256 JWT verification and the public key into the executable, so verification is part of the build rather than a second product you integrate and maintain.

Worth knowing before you start: module names must be unique across your source files, and __init__.py files are skipped during compilation because they’re namespace markers rather than compilable code.

FAQ

Does compiling Python make it impossible to reverse engineer?

No. Compilation removes readable source, comments, and names, and turns your logic into machine code, which makes recovery far more expensive and time-consuming. It raises the cost rather than eliminating the possibility, which is usually the practical goal for proprietary software.

Is shipping .pyc files enough protection?

Generally not. Python bytecode can be decompiled back to something close to the original source with widely available tools, so .pyc is a weak barrier. Compiling to native machine code removes the bytecode layer entirely.

Do I need to write Cython or C extensions to protect my code?

Not with Py2Native. You write plain Python and run uv run py2native build main.py *.py --wheel dist. Cython is used under the hood, but there are no .pyx/.pxd files for your application code, no manual cythonize step, and no C extensions to write.

Can I add license verification to a compiled Python app?

Yes. The Pro plugin lets you call _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT") from your code, and it bakes the signature verification logic and public key into the executable.

The Bottom Line

The reason compiled Python is harder to reverse engineer is not that it’s magic — it’s that you’ve removed every affordance that made your source easy to read. Names, comments, structure, and the text itself are gone, replaced by machine code that has to be reconstructed by hand. That’s a meaningful wall for anyone shipping proprietary logic into environments they don’t control.

Py2Native gives you that wall without the usual tax: plain Python in, native binary out, dependencies untouched, and an MIT-licensed core you can audit. When you need enforcement rather than just obfuscation, the Pro plugin adds signed JWT license verification compiled into the same artifact.

uv run py2native build main.py *.py --wheel dist

Start with one command on your existing project — no rewrite, no .pyx files, no build system overhaul. Full details are at py2native.dev.

Related posts

EU label: AI-generated content