What Does Cython Actually Do to Your Python Code?
If you write Python for a living, you’ve probably heard the sentence “Cython compiles Python to C” at least a dozen times — usually right before someone tells you it will also protect your source code. Both halves of that sentence are true, but the details matter a lot if you’re shipping proprietary software. What exactly does Cython do to your Python code, and what does the output actually look like on disk?
This is the explainer we wish we’d had before shipping our first compiled Python binary.
The Short Answer: Cython Turns Python Into C
Cython is a transpiler, not an interpreter and not a bundler. It reads Python source (or Cython-flavored Python with extra type annotations) and emits C source code as its output.
From there, the pipeline is completely ordinary:
- Cython writes
.cfiles next to your sources. - A platform C compiler (MSVC, GCC, or Clang) compiles those
.cfiles into object files. - A linker produces a native shared library (
.pydon Windows,.soon Linux,.dylibon macOS) or a standalone executable.
The important consequence: the result is not “encrypted Python.” There is no scrambled bytecode to unpack. It’s machine code that the CPU executes directly. If someone wants your logic, they’re looking at disassembly — not at .py files or easily-decompiled .pyc bytecode.
That’s exactly why Cython sits underneath many Python-to-native tools, including Py2Native.
A Real-World Analogy: Translating a Recipe Into Assembly Instructions
Think of your Python source as a recipe written for a human cook: “fold in the eggs, whisk until smooth, bake for 25 minutes.”
Cython is the technical writer who translates that recipe into the machine instructions a factory line follows: rotate the mixer at 400 rpm, load tray 12, hold at 180°C for 1,500 seconds.
The ingredients never change. Every if, every loop, every call to requests.get() is still there — same behavior, same bug-for-bug semantics. What changes is the format: from prose a human reads to instructions only the machine follows.
And once the recipe is on the assembly line, you can’t easily reconstruct the original prose from watching the conveyor belt. For proprietary code, that is the whole point.
Under the Hood: The Cython Compilation Pipeline
Here’s the sequence, in the order it actually happens.
Step 1 — Cython parses .py files and generates C files. In embedded mode, Cython also needs an entry point, which is why the toolchain generates a bootstrap.pyx from templates and invokes Cython with --embed --shared --embed-modules. All your .pyx/.py files become .c.
Step 2 — A C compiler turns the C into object files. Platform flags matter here: include paths for the CPython headers, optimization levels, and the correct runtime library for your target.
Step 3 — The linker produces a shared library or an executable. Library mode calls link_shared_object(); executable mode calls link_executable(). The output is .pyd, .so, or .dylib, or a runnable binary.
One thing that surprises people: Cython does not remove CPython. Dynamic features (attribute lookups, import, reflection) still call into the interpreter at runtime, so the CPython runtime has to be present — or embedded into the deployment directory alongside your binary.
Optional stages on top of that: creating a PEP 427 wheel with the compiled .pyd/.so inside, or building a uv-managed deployment directory that ships the interpreter and your native modules together.
Why Developers Care: Protection, Performance, and Portability
Three reasons, in descending order of how much they actually matter for a commercial app.
- Protection. Compiled machine code is dramatically harder to reverse-engineer than
.pyor.pyc. There’s nouncompyle6for a stripped.so. This is the primary motivation for most teams. - Performance. Cython can absolutely speed up hot loops, especially with type annotations. But if you’re compiling an application for distribution, the win is protection, not throughput — most apps aren’t CPU-bound in the code you’d compile.
- Portability. The same plain Python source can be compiled for Windows, Linux, and macOS, each with platform-specific flags handled by the toolchain.
And the misconception worth clearing up right here: Cython does not make Python “fully compiled” in the sense of eliminating the runtime. It compiles your modules to native code that still interacts with CPython.
The Catch: Using Cython Directly Is a Build-System Project
Raw Cython works. It’s also a project in itself.
- You write
.pyxand.pxdfiles, which means learning Cython’s syntax —cdef,cpdef,cimport, typed memoryviews, the works. - You manage
setup.py(or a build backend config) and invokecythonizeyourself:cythonize -i mymodule.pyx. - You handle platform-specific compiler flags, include paths, runtime library discovery, and linking for every OS you ship to.
- You handle packaging — wheels, tags, and on Linux the
auditwheel repairstep for manylinux compliance. - You still manage every third-party dependency, because Cython only compiles your modules. Dependencies stay as Python source.
That’s the hard way. It works, and plenty of teams have done it. But “zero-config” it is not.
There’s also a quieter constraint that bites people: you can’t have modules with identical names across your source files, because they all end up in one compiled namespace.
The Zero-Config Alternative: Py2Native Compiles Plain Python
Py2Native uses Cython under the hood and hides it completely. You write plain Python — no .pyx, no .pxd, no .c files, no manual cythonize call, no C extensions to write.
uv run py2native build main.py *.py --wheel dist
That single command expands your glob patterns, generates the Cython bootstrap, transpiles to C, compiles with the platform toolchain (MSVC/GCC/Clang), links the shared object, and produces a PEP 427 wheel in dist/. Third-party libraries are left unmodified and work as-is with minimal or no configuration.
Other useful flags:
uv run py2native build main.py *.py --library --wheel dist # shared library + wheel
uv run py2native build main.py *.py --embed deploy # uv-managed deploy dir
uv run py2native build main.py *.py --no-console # windowed app on Windows
In library mode the wheel contains a single compiled shared library plus two generated files: __init__.py, a meta-path finder that redirects import mypkg.submodule to the native library, and __main__.py, which makes python -m mypkg work. Both are required. (We dig into that setup in How to Compile Python to a Shared Library Instead of an Executable.)
Adding signed license enforcement with the Pro plugin
The Pro plugin (py2nativepro) adds ES256 JWT license verification on top of the open-source compiler. Only the public key is stored in the executable, and verification is handled by compiled code inside the product — no third-party crypto libraries are pulled in.
The workflow is three commands:
# 1. Generate an EC P-256 keypair
uv run py2native keygen private.pem public.pem
# 2. Sign a JWT payload with the private key -> license.dat
uv run py2native sign --private private.pem payload.json license.dat
# 3. Build with the license requirement baked in
uv run py2native build main.py *.py --library --license license.dat --public public.pem
You can inspect claims at any time:
uv run py2native show --public public.pem license.dat
To enforce the license in your own code, add a declaration file next to your sources. This is the one place you touch Cython-flavored syntax — a declaration, not a rewrite, and everything else in your app stays plain Python:
# _p2n_bootstrap.pxd
# Declares the verification hook that the Pro plugin bakes into the build.
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
Then call it from your verification module:
# license_check.pyx
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
def check_license(licenseString):
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
return license
And gate your application’s real work on the result:
from license_check import check_license
with open("license.dat") as f:
license_string = f.read()
claims = check_license(license_string)
start_app(claims)
Because the issuer and audience are pinned at the call site — RSJ Software GmbH and TimestampGIT — a token signed by anyone else simply won’t verify. We cover the operational side of this in Verifying a Signed JWT License Inside a Compiled Python App.
Common Misconceptions About Cython and Code Protection
“Cython obfuscates code.” No. Obfuscation mangles names and control flow while keeping bytecode intact. Cython compiles to native code, which is a fundamentally different and stronger protection layer.
“Cython removes the need for the Python runtime.” No. CPython is still required unless you embed it — which is what the --embed mode does, producing a uv-managed deployment directory.
“Cython protects my third-party libraries.” No. Only your custom code gets compiled. Dependencies stay as Python source, monkey-patching is still possible against them, and LGPL libraries remain replaceable by the end user — which is what keeps you compliant.
“I have to rewrite my code in Cython syntax.” Not with a tool like Py2Native. You keep plain Python and let the compiler handle the transpilation, platform flags, linking, and packaging.
FAQ
Does Cython convert Python to machine code?
Cython transpiles Python to C, and then a C compiler turns that C into machine code. So yes, the final output is native machine code, but the process goes through C as an intermediate step.
Can I use Cython without writing .pyx files?
Yes. Tools like Py2Native use Cython under the hood but let you write plain Python. You do not need to write .pyx or .pxd files or run cythonize manually.
Does Cython protect my Python source code from reverse engineering?
Compiling to native code makes reverse engineering significantly harder than reading .py or .pyc files, but no protection is absolute. Cython is a strong layer; combining it with license checks and other measures improves security.
How does Py2Native differ from using Cython directly?
Py2Native is a zero-config compiler that wraps Cython. You write plain Python, run uv run py2native build, and get a native binary or wheel. It handles platform flags, linking, and packaging for you, and the Pro plugin adds signed JWT license verification.
The Bottom Line
Cython is a transpiler: it takes Python, emits C, and lets a C compiler turn that C into machine code. That’s a genuinely strong answer to “how do I stop people from reading my source?” — stronger than obfuscation, because there’s no bytecode left to recover.
The cost is that driving Cython directly is a build-system project: .pyx files, setup.py, cythonize, platform flags, wheel tags, auditwheel. If all you want is a compiled binary from the Python you already wrote, that’s overhead you don’t need to take on.
Py2Native collapses that project into one command — uv run py2native build main.py *.py --wheel dist — and the Pro plugin adds ES256 JWT license enforcement with the public key and verification logic compiled straight into your binary. If you’re comparing approaches before committing, start with Python Code Protection Tools Compared: Py2Native vs Obfuscators vs Cython, then run a build against your own codebase and inspect the output yourself.
Related posts
- How to Compile Python to a Shared Library Instead of an Executable
- Python Code Protection Tools Compared: Py2Native vs Obfuscators vs Cython
- Verifying a Signed JWT License Inside a Compiled Python App