What Is Cython, and Do You Need It to Compile Python?
You decided to ship a compiled binary instead of readable .py files. Someone in the issue tracker said “just use Cython.” Now you’re reading about .pyx files, setup.py hooks, and type annotations you never wrote, wondering whether this is actually required — or whether there’s a way to compile plain Python as-is.
Short version: Cython is the engine underneath most Python-to-native compilation, but you don’t have to drive it manually. This article explains what Cython actually is, what it does and doesn’t do for you, and how to get native, protected Python without writing a single line of Cython syntax.
What Is Cython? A Plain-Language Explanation
Cython is a Python-to-C transpiler. It reads Python-like source code and emits C source, which a normal C compiler then turns into machine code — a shared library (.so, .pyd, .dylib) or an executable.
That’s the whole job. It is not a Python interpreter, not a runtime, and not a packager. It’s a build-time tool that sits between your source file and your platform’s C compiler.
A useful analogy: Cython is a translator who will happily translate for you — but gets much better results if you speak their dialect. That dialect means .pyx files, cdef/cpdef declarations, static type annotations, and .pxd header files that describe what crosses module boundaries. You can hand a translator plain speech too, but then you own the plumbing around the conversation: build scripts, include paths, linker flags, packaging.
Two more things worth pinning down:
- Cython requires a C compiler. On Windows that means MSVC, on Linux GCC, on macOS Clang. Cython generates C; something else has to compile it.
- Cython was built for performance, not protection. It exists to let you write C extensions with Python-ish syntax and get C-level speed. Using it as a code-protection mechanism is a later, somewhat accidental use case — and it’s why the workflow feels like it was designed for someone else.
Cython is a mature, widely used project (Py2Native itself depends on cython >= 3.2.4), and none of the above is a criticism. It’s just context for the question below.
How Cython Works Under the Hood (and Why It Feels Complicated)
The canonical Cython workflow goes roughly like this:
- Rename or author your modules as
.pyxfiles. - Add type annotations and, for anything shared across modules, matching
.pxddeclaration files. - Write a
setup.py(or invokecythonizedirectly) to drive the transpilation. - Compile the generated
.cfiles with platform-appropriate flags. - Link them against the right CPython shared library for each OS and architecture.
- Package the result yourself — wheel, directory layout, entry point.
Even the abbreviated version shows the shape of the problem:
# The "hard way": you own every step below.
cythonize -i main.pyx # .pyx -> .c -> platform-specific shared object
That one line hides a lot. In practice you also manage:
- Per-platform build configuration. Windows wants different flags and
.pydnaming than Linux’s ELF.so, which differs again from macOS Mach-O.dylib. Auditwheel repair on Linux is a separate chore. - Import and packaging glue. Cython compiles modules; it does not decide how
import mypkg.submoduleresolves to your compiled artifact, or howpython -m mypkgfinds an entry point. - Embedding glue. If you want a standalone executable rather than an importable extension, you write the bootstrap code that starts the interpreter and calls into your module.
And yes — Cython can consume plain .py files. But “can compile a .py file” is a long way from “produces a distributable, protected application.” The distance between those two is exactly the glue code above, and it’s the reason so many teams abandon the manual route halfway through.
This is the gap Py2Native fills: it uses Cython as its transpiler, then automates everything around it.
Do You Need Cython to Compile Python? The Short Answer
No. You do not need to learn Cython, write .pyx files, author .pxd headers, or maintain build scripts to compile Python into native machine code.
You need something that does the transpiling — and tools like Py2Native use Cython for exactly that, internally. What you don’t need is to operate it.
Here’s the contrast, side by side:
| Manual Cython | Py2Native | |
|---|---|---|
| Source format | .pyx + .pxd files |
plain .py files |
| Build config | setup.py / cythonize per platform |
one CLI command |
| Platform flags | yours to maintain | handled for Windows, Linux, macOS |
| Packaging | wheel + glue by hand | --wheel, --library, --embed |
| Entry point | hand-written bootstrap | generated from templates |
The Py2Native equivalent of that entire pipeline is:
uv run py2native build main.py *.py
Glob your sources in, get native output. Add the distribution modes you need:
# Shared library + wheel + uv-managed deployment directory
uv run py2native build main.py *.py --library --wheel dist --embed deploy
--library produces a shared library instead of an executable, --wheel writes a PEP 427 wheel containing the compiled artifact, and --embed creates a uv-managed deployment directory. --no-console drops the console window on Windows builds, and --base <dir> controls where glob patterns are expanded from.
No .pyx. No cythonize. No per-OS flag table in your repo.
Common Misconceptions About Cython and Python Compilation
“Cython makes your code completely unreadable.” Cython-generated C is machine-oriented, but it’s still C — it can be inspected and reverse-engineered given enough effort. Protection isn’t a property you get for free from a transpiler; it depends on how you compile and distribute, and on what extra layers you add. Treat compiled output as a strong barrier, not an absolute one.
“You must rewrite your code in Cython syntax to get native code.”
This is the misconception that stops most teams. You don’t. Py2Native generates its own bootstrap.pyx from internal templates and compiles your plain Python modules — you never write Cython-flavored code, and there’s no manual cythonize step in your build.
“Cython is only for performance.” It’s primarily for performance, and that’s what its design optimizes for. It can be pressed into service for protection, but it isn’t a dedicated protection tool — it has no notion of license keys, string obfuscation, or per-customer builds. Py2Native adds those: the Pro plugin layers JWT license verification and string compression on top of the compiled output.
“Compiling my code also compiles my dependencies.” With Py2Native, third-party Python libraries are left unmodified and work as-is. Only your custom code is compiled to native machine code. Libraries remain as Python source, which has two consequences: dependency setup stays simple, and monkey-patching remains possible. LGPL libraries also stay replaceable by the end user, which keeps you compliant.
One more constraint worth knowing up front: no two source files may share a module name, and __init__.py files are skipped during compilation since they’re namespace markers rather than compilable code.
How Py2Native Compiles Plain Python Without Cython Syntax
Under the hood, the build orchestrator runs a fixed pipeline. Knowing it helps when something goes sideways.
- Glob sources —
build.pyexpands the glob patterns you passed, relative to--base. - Plugin dispatch —
pluginManager.extendBuild(args)runs lifecycle hooks; the Pro plugin performs its license checks here. - Cython → C —
compiler.pygeneratesbootstrap.pyxfromtemplates.py, invokes Cython, and transpiles every.pyxto C. - C → objects — the setuptools distutils compiler interface compiles each
.cfile with the correct platform flags. - Link —
link_shared_object()for library mode,link_executable()for executable mode, against the located CPython shared library. - Wheel (optional) —
whl.pybuilds a PEP 427 wheel containing the compiled.pyd/.so. - Embed (optional) —
embed.pyproduces auv-managed deployment directory.
The plugin table is short and platform-shaped:
| Plugin module | Role |
|---|---|
py2nativepro |
JWT license verification, string compression, keygen/sign/show |
.winplatform |
Windows compile/link flags, PE linking, .pyd naming |
.linuxplatform |
ELF linking, auditwheel repair, .so naming |
.macplatform |
Mach-O linking, .dylib naming |
In library mode, the wheel ships two Python files alongside the compiled shared library, and both are required: __init__.py is a meta-path finder that redirects import mypkg.submodule into the native library, and __main__.py makes python -m mypkg work. Because the wheel builder generates __main__.py, it’s excluded from your source list during compilation.
If you want the deeper comparison of compilation strategies, see Nuitka vs Cython Compilation: Which Protects Python Source Better?.
Adding License Verification to Your Compiled Python (Pro Feature)
Compilation protects your source. It doesn’t tell you whether a given copy is allowed to run. That’s the Pro plugin’s job: it adds signed-JWT license verification, backed by an EC P-256 keypair.
Start by generating a keypair:
uv run py2native keygen private.pem public.pem
Sign a payload into a license file, and inspect what you signed:
uv run py2native sign --private private.pem payload.json license.dat
uv run py2native show --public public.pem license.dat
Then build with the license and public key wired in:
uv run py2native build main.py *.py --license license.dat --public public.pem
The --license flag requires a valid Pro license to build. At link time, the public key is baked into the executable and the signature-verification code is compiled in — the elliptic-curve verification is implemented in compiled code inside the software, with no third-party crypto libraries involved. Only the public key ships; the private key never leaves your build machine.
To call the verification routine from your own compiled module, you declare its signature once in a .pxd file:
# _p2n_license.pxd
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
And call it where your application would otherwise proceed:
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
licenseString = read_license_file("license.dat")
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
Pass your real expected issuer and audience values as the expected_iss and expected_aud keyword arguments — the call raises if the token doesn’t match, and the check happens inside compiled code that ships with your binary. For the end-to-end walkthrough, including what to do when verification fails, see Verify a Signed License Key Inside a Compiled Python Executable.
Requirements for the whole toolchain, if you’re checking compatibility: CPython 3.11–3.15 (including free-threaded 3.14t and 3.15t), a platform C compiler, Windows 8+ / manylinux2014 or musl Linux / macOS, x86-64 or ARM64, and internet access so uv can download Python distributions and libraries.
FAQ
Can I compile Python to native code without learning Cython?
Yes. Py2Native lets you compile plain Python code to native machine code without writing any Cython syntax. It uses Cython under the hood but automates the entire process, so you just run uv run py2native build.
Is Cython required for code protection? No. While Cython can be used for protection, it requires manual steps and Cython-specific syntax. Py2Native provides a zero-config way to protect your Python code by compiling it to native binaries, with optional license verification via the Pro plugin.
How does Py2Native handle third-party libraries? Py2Native leaves third-party Python libraries unmodified and works with them as-is. Only your custom Python code is compiled to native machine code, so libraries remain as Python source, which means monkey-patching is still possible.
Do I need the Pro plugin for license verification? Yes, license verification is a Pro feature. The Pro plugin adds JWT license key generation, signing, and verification, with the public key and verification code baked into your compiled executable.
Is Py2Native’s core open source? Yes. The compiler itself is MIT licensed. The Pro plugin is proprietary and adds JWT license verification and string compression.
Conclusion
Cython is excellent at what it was built for: writing fast C extensions with Python-flavored syntax. It is not a packaging system, not a runtime, and not a protection product — and the manual workflow around it is where most teams get stuck.
You don’t need to learn it to ship compiled Python. Write the plain Python you already have, run one command, and let Py2Native handle the transpilation, platform linking, wheel building, and uv-managed embedding:
uv run py2native build main.py *.py --wheel dist --embed deploy
When you’re ready to control who can run those binaries, add the Pro plugin for signed JWT licenses. Start at py2native.dev and see Compile Plain Python to Native Code Without Rewriting in C for the module-level details.
Related posts
- Best Python Code Obfuscation and Protection Tools
- Python Code Protection for Commercial Software Vendors
- Nuitka vs Cython Compilation: Which Protects Python Source Better?