Py2Native Python to native compilerPython-zu-Native-Compiler

← All posts

2026-11-02

Is Compiled Python Really Unreadable? An Honest Look

Is Compiled Python Really Unreadable? An Honest Look
python compiler cython code protection open source

Is Compiled Python Really Unreadable? An Honest Look

If you search for “is compiled Python really unreadable,” you’ll get two kinds of answers: marketing that says “yes, it’s a black box” and sceptics who say “no, everything is crackable.” Both are partly right, and neither is useful when you’re the one deciding whether to ship a .pyc or a native binary to a customer who shouldn’t see your pricing engine.

This article is the honest version. We’ll look at what “compiled” actually means for Python, what an attacker can realistically recover from each distribution format, and where the line between “inconvenient” and “protected” really sits. No hand-waving, no claims of perfect secrecy.

The Short Answer: It Depends on How You Compile It

Compiled Python is not magically unreadable. What compilation does is remove your original source code from the artifact and raise the cost of reverse engineering it. Whether that cost is “a coffee break” or “a specialist-week” depends entirely on which compilation you mean.

Roughly, there’s a spectrum:

  • Plain .py source — fully readable. Comments, variable names, business logic, todo notes about the customer. Everything.
  • .pyc bytecode (what you get from python -m compileall, and what bundlers like PyInstaller ship) — not human-readable text, but trivially decompilable. Tools such as uncompyle6 reconstruct near-original source, including most names and structure.
  • Obfuscated bytecode — the same bytecode with names mangled and control flow mangled. It slows a reader down; it doesn’t change what’s technically recoverable.
  • Native machine code — what a real Python-to-native compiler produces. There is no Python-level structure left to decompile. Disassembly gives you instructions, not calculate_discount().

So the useful framing is: the practical goal is protecting your proprietary logic, not achieving perfect secrecy. Any binary can be reverse engineered by someone determined enough. The question you should be asking is whether the effort required is disproportionate to the value of what’s inside — because that’s the only threshold that has ever mattered in software protection.

A Real-World Analogy: Locked vs. Shredded Documents

Imagine you’ve written a confidential operating manual and you need to hand it to a client.

Shipping .py files is handing over the manuscript. Your client is trusted not to read it, but nothing physically stops them.

Shipping .pyc files is putting the manuscript in a locked box. It feels safer — until you notice the lock is a standard model, the key is published in the manual that comes with the box, and there’s a well-maintained tool whose entire purpose is opening exactly this box. That’s bytecode: locked against a casual glance, open to anyone who knows the format.

Shipping native code is shredding the manuscript and handing over a machine that performs the same actions the manual described. The client can run the machine, measure it, take it apart, and watch which gears turn. But they can’t read your prose, because the prose isn’t in there anymore.

The analogy breaks down in one important place, and it’s the place most vendors gloss over: third-party libraries stay as readable Python source. Your dependency on a public open-source package doesn’t get shredded. The machine contains your shredded logic; the library next to it is still a manuscript anyone can read.

Under the Hood: What Actually Happens to Your Python Code

Standard Python execution is a two-step process: your source is parsed and translated into bytecode, then the interpreter executes that bytecode. Bytecode is a compact instruction set for a stack machine, and critically, it retains names, constants, docstrings, and the shape of your functions. That’s why decompilation works — the information was never discarded, only encoded.

A native compiler changes the endpoint. Instead of stopping at bytecode, it takes a route through C:

  1. Your plain Python modules are translated into C source.
  2. A platform C compiler (MSVC, GCC, Clang) turns that C into object files.
  3. The linker produces a shared library or executable that talks to the CPython runtime.

Under the hood, Py2Native does this with Cython — but that detail is deliberately hidden. You write plain Python. There are no .pyx files to author, no .pxd headers to maintain for your business logic, no manual cythonize step, and no C extension to hand-write. The whole pipeline is one command:

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

That’s it. The build orchestrator globs your sources, dispatches to the platform plugin for the right compiler and linker flags, generates a bootstrap, runs the Cython and C stages, and drops a runnable deployment directory in dist.

The important asymmetry: only your custom code becomes native. Third-party libraries are left unmodified and work as-is with minimal or no configuration. That’s a deliberate trade-off — it keeps dependency compatibility intact, and it means you’re not fighting a build system every time a package updates.

What Attackers Can and Can’t Recover

Let’s be concrete instead of reassuring.

What an attacker can recover from a native binary:

  • String literals that survived into the binary — file paths, format strings, error messages, URLs.
  • Imported module and library names, which show up in the linking and runtime metadata.
  • Control-flow patterns via disassembly: loops, branches, calls into the CPython API.
  • Any embedded data you shipped inside the executable, including keys or configuration blobs.
  • Runtime behaviour: attach a debugger, set breakpoints, observe the values flowing through your functions.

What they cannot easily recover:

  • Your original source code, as source.
  • Variable names, function names, and class names that didn’t survive as symbols.
  • Comments and docstrings — these are parsed away long before compilation.
  • The high-level Python structure: which function called which, expressed in Python terms.

Two caveats worth stating plainly:

Monkey-patching is still possible. Because third-party libraries remain Python source, an attacker can still patch a library that your compiled code imports. If your protection depends on “the attacker can’t modify request handling,” native compilation of your module doesn’t cover that.

A determined attacker can still reverse engineer native code. This is a well-established discipline. The barrier isn’t “impossible,” it’s “expensive, specialised, and time-consuming” — which is a very different and much more honest claim than the one you’ll find on most vendor pages.

Common Misconceptions About Compiled Python

Misconception: “Bundling to a .exe with PyInstaller protects my code.” It doesn’t. PyInstaller packages your code as .pyc bytecode inside an executable, and those bytecode files can be extracted and decompiled back to near-original source. You shipped a locked box with a published key. It’s a convenient distribution format, not a protection mechanism.

Misconception: “Obfuscation is enough.” Obfuscated bytecode still runs on the standard interpreter, so the interpreter must still be able to understand it — and so can a sufficiently motivated person. Obfuscation raises the time cost from minutes to hours. That’s a real improvement, but it’s not the same category of change as removing the bytecode entirely. If you want the longer comparison, see Best Python Code Protection Tools in 2026: Obfuscators vs Compilers.

Misconception: “Native compilation makes code impossible to crack.” No. It removes your source, strips the Python-level structure, and makes the cost of analysis go up by orders of magnitude. Anyone claiming absolute protection is either mistaken or selling something. The correct claim is: there is nothing left to decompile back to Python.

Misconception: “I need to write Cython or C to protect my code.” This is the one that stops most teams. Yes, you could hand-annotate .pyx files, write a build script, and manage a manual cythonize step — that’s the hard way, and it’s a maintenance project that never ends. You don’t have to. Tools like Py2Native let you keep writing plain Python and handle the translation for you. If you want to see how the alternatives compare, Cython Alternatives for Protecting Python Code covers the landscape.

How Py2Native Makes Protection Practical

Py2Native is a zero-config Python-to-native compiler. It compiles your custom Python into native machine code so you can ship a protected binary instead of readable source — and it hides Cython completely behind a single command. The core compiler is MIT licensed and open source, which matters if you have a security review process.

The basic workflow, producing a uv-managed deployment directory:

uv sync
uv run py2native build main.py *.py --embed dist
./dist/main

For library-style distributions, --library compiles everything into a single shared library wrapped in a PEP 427 wheel with the two files that bridge Python’s import system to the native code. That scenario is covered in depth in Python Licensing for On-Premise Deployments.

Adding license verification with the Pro plugin

The open-source core compiles. The commercial Pro plugin (py2nativepro) adds signed JWT license verification, with a key property: only the public key is stored in the executable, and the elliptic-curve signature verification is handled by compiled code baked into your binary — no third-party crypto library required at runtime.

Generate a keypair and sign a license:

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

show prints the JWT claims and can verify the signature against the public key, which is handy for support tickets.

Then build with the license baked in:

uv run py2native build main.py *.py --embed dist \
  --license license.dat --public public.pem

To call the verification logic from your own code, you include a declaration file next to your sources. This is the one place you drop below plain Python — it’s the boundary where your compiled module meets the verifier:

# license_gate.pxd
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)

And the call site, which imports the runtime entry point the plugin provides:

from _p2n_bootstrap cimport _runtime_verify_es256_jwt

licenseString = read_license_from_disk()  # your own loader
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")

If the verification fails, you decide what happens — degrade the feature, refuse to start, limit to a trial tier. The check runs inside your native binary, so there’s no readable Python branch for someone to patch out with a text editor. You can read more about the pattern in Verify a License Key Inside a Compiled Python Binary.

Builds like this belong in CI, not on a developer laptop. Automate Compiling Python to Native Binaries in CI/CD walks through the pipeline.

FAQ

Can compiled Python be decompiled back to source? Native compiled Python cannot be decompiled back to the original source code. Tools like uncompyle6 can decompile .pyc bytecode, but native machine code produced by compilers like Py2Native does not contain the original Python structure, making decompilation infeasible. However, determined attackers can still reverse engineer the binary with disassemblers, so it’s not impossible — just much harder.

Is PyInstaller enough to protect my Python code? No. PyInstaller bundles your code as .pyc bytecode inside an executable, but those .pyc files can be extracted and decompiled back to near-original source. For real protection, you need to compile your Python to native machine code, which is what Py2Native does.

Do I need to know Cython to use Py2Native? No. Py2Native uses Cython under the hood but hides it completely. You write plain Python — no .pyx files, no manual cythonize step, no C extensions. Just run uv run py2native build and it handles the compilation pipeline for you.

How does Py2Native handle third-party libraries? Third-party libraries are left unmodified and work as-is with minimal or no configuration. Only your custom Python code is compiled to native machine code, so you don’t need to worry about compatibility issues with dependencies.

The Honest Bottom Line

Compiled Python is not unreadable in some absolute sense — it’s unreadable as Python, and that’s the property that actually matters. Bytecode hands an attacker your source back with a decompiler and ten minutes. Native machine code hands them assembly and a much longer project, with your names, comments, and structure gone.

That’s the right trade for proprietary logic. Just go in knowing what it does and doesn’t cover: your code becomes native, your dependencies stay Python, and the bar goes up rather than to infinity.

Ready to stop shipping readable source? Start with Py2Native — uv run py2native build main.py *.py --embed dist — and see what your deployment directory looks like when there’s no .py file left in it. If you need per-customer licensing, add the Pro plugin and sign your first JWT.

Related posts

EU label: AI-generated content