Compile Python to a Native Binary Without Writing a Single .pyx File
You want to ship a Python binary instead of readable .py files. You’ve read that Cython is how you do it — and then you hit a wall: .pyx files, .pxd declaration files, a manual cythonize step, a setup.py that builds C extensions, and a build system that breaks the moment a third-party dependency doesn’t cooperate.
Here’s the thing: you can compile plain Python to a native binary without ever touching Cython syntax. This walkthrough takes a normal Python project — main.py plus a handful of modules, zero .pyx files — and produces a compiled executable, a PEP 427 wheel, or a shared library. Every command runs through uv run py2native. No pip, no cythonize, no hand-written C.
Why You Don’t Need Cython Syntax to Get a Native Binary
The misconception is understandable. Cython is genuinely the engine doing the heavy lifting: it transpiles Python into C, which then compiles into machine code. So people assume the price of admission is learning Cython’s type syntax and build model.
That’s the hard way. Writing .pyx and .pxd files by hand, invoking cythonize yourself, wiring up Extension() objects in a setup.py, and debugging linker errors — that’s a workflow designed for people writing C extensions, not for people protecting an application’s source code. If you want the deeper background on what Cython actually is, we covered it separately in What Is Cython, and Do You Need It to Compile Python?.
Py2Native’s entire premise is that the Cython step should be invisible. You keep writing plain Python. During the build, the compiler generates a bootstrap.pyx from its own templates, invokes Cython with --embed --shared --embed-modules, compiles every .pyx to C, compiles the C with platform-correct flags, and links the result against the CPython shared library it locates for you. You never see the generated .pyx — and you never write one.
Equally important: third-party Python libraries are left unmodified and work as-is with minimal or no configuration. That’s the difference between Py2Native and protection tools that ask you to hand-maintain a build configuration for every dependency in your tree.
Prerequisites: What You Need Before the First Build
Py2Native compiles natively for the platform you’re on, so the toolchain requirements are the same ones any native build has:
- CPython 3.11–3.15, including the free-threaded
3.14tand3.15tbuilds. - A platform C compiler and linker — MSVC on Windows, GCC on Linux, Clang on macOS.
- A supported OS and CPU — Windows 8+, manylinux2014/musl Linux, or macOS, on x86-64 or ARM64.
- Internet access during the build. Py2Native downloads the Python distributions and libraries it needs.
uv, installed and on yourPATH. Check with:
uv --version
The open-source core depends on cython >= 3.2.4, setuptools >= 82.0.1, and uv >= 0.11.8 (plus auditwheel >= 6.6.0 on Linux, used to repair wheels for manylinux compliance). You don’t manage those by hand — the compiler handles its own build environment.
And notably: no Cython knowledge is required. Bring your existing Python project as it is.
Step-by-Step: Compile Plain Python to a Native Binary
Step 1: Prepare your project
Lay out your sources like you always would:
myapp/
├── main.py
├── config.py
├── report.py
└── fetch.py
Two known constraints to check before building:
- No duplicate module names across source files. If you have
utils.pyin two different directories and glob them into the same build, rename or restructure. The compiler maps modules by name, soa/utils.pyandb/utils.pycollide. __init__.pyis skipped during compilation. It’s a namespace marker, not compilable code — don’t put logic there.
If you’re building in library mode, don’t add your own __main__.py; the wheel builder generates it.
Step 2: Run the build
From the project root:
uv run py2native build main.py *.py
That’s the whole thing. To also produce a uv-managed deployment directory:
uv run py2native build main.py *.py --embed dist/ --base .
Or a PEP 427 wheel with the compiled .pyd/.so inside:
uv run py2native build main.py *.py --wheel dist/
The --base <dir> flag tells the build where to expand glob patterns. --no-console builds a windowed executable if you’re shipping a GUI.
Step 3: Understand what just happened under the hood
No magic, just orchestration. The build pipeline runs in this order:
- Glob sources —
build.pyexpands your patterns under--base. - Plugin dispatch —
pluginManager.extendBuild(args)loads the platform plugin (and the Pro plugin, if present) and lets it run license checks. - Cython → C —
compiler.pygeneratesbootstrap.pyxfromtemplates.py, then compiles your plain.pysources to C. - C → objects — the platform compiler builds
.cfiles with the right flags for Windows, Linux, or macOS. - Link —
link_shared_object()for library mode,link_executable()for executable mode, against the CPython shared libraryruntime.pyfound for you. - Wheel (optional) —
whl.pypackages the compiled artifact. - Embed (optional) —
embed.pycreates theuv-managed deployment directory.
The value isn’t that this is complicated — it’s that you don’t maintain it.
Step 4: Build a library instead of an executable
If you’re shipping an importable package rather than a standalone binary:
uv run py2native build main.py myapp/*.py --library --wheel dist/
This produces a shared library — .pyd on Windows, .so on Linux, .dylib on macOS — containing all your modules. The wheel adds exactly two generated files:
__init__.py— a meta-path finder that redirectsimport myapp.submoduleinto the compiled shared library.__main__.py— the entry point that makespython -m myappwork.
Both are required. Skip __init__.py and Python’s import system has no bridge to the native library; skip __main__.py and there’s no runnable entry point.
Step 5 (optional): Bake in license verification with the Pro plugin
The core compiler is open source (MIT). The py2nativepro plugin is the commercial add-on, and it’s where signed-license enforcement lives. It gives you three commands:
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
keygen generates an EC P-256 keypair. sign signs a JWT payload and writes license.dat. show displays the claims, optionally verifying the signature against the public key.
To enforce the license at runtime, add a .pxd file to your project declaring the verification signature:
# _p2n_bootstrap.pxd
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
Then call it in your code:
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
licenseString = open("license.dat").read()
license = _runtime_verify_es256_jwt(
licenseString,
expected_iss="RSJ Software GmbH",
expected_aud="TimestampGIT",
)
if not license:
raise SystemExit("No valid license found.")
Build with the license wiring enabled:
uv run py2native build main.py *.py --license license.dat --public public.pem --wheel dist/
The Pro plugin bakes the signature verification code and the public key directly into the executable. Only the public key ships — the private key never leaves your build machine. Elliptic curve verification is handled with compiled code in the plugin itself, with no third-party crypto libraries in the binary. That’s the maximum-security, maximum-flexibility option: your licensing logic is native code, not Python that can be patched out.
This is the one place where a Cython-flavored construct appears — and it’s a declaration file plus a single call, generated and compiled by the plugin. Everything else in your project stays plain Python.
Step 6: Distribute
Ship the executable, or uv-install the embedded deployment directory, or publish the wheel. No .pyx files were written by you at any point. For automating the wheel step on every release, see Ship Compiled Python Wheels Automatically on Every Release.
How to Confirm It Worked
1. Run it. Execute the compiled binary directly, or install the wheel and import the package. Behavior should be identical to running your source — same output, same exit codes, same edge cases.
2. Inspect the output directory. You should see a native binary (or .pyd/.so/.dylib) and no readable .py source for your custom modules. If your report.py is still sitting there as text, the glob didn’t cover it — check --base.
3. Test library mode. Run python -m myapp and confirm imports resolve through the generated __init__.py meta-path finder:
uv run python -m myapp
4. Test the license gate. If you built with the Pro plugin, run once with a valid license.dat — it should proceed. Then tamper with the token (change a character in the payload) and run again. It should refuse to start. That confirms the embedded verification logic is actually executing, not being skipped.
Troubleshooting Common Issues
Build fails with a missing compiler. MSVC, GCC, or Clang isn’t installed or isn’t on PATH. Install the platform toolchain and reopen your shell so the environment refreshes.
Duplicate module names. Two files named config.py in different directories globbed into one build. Rename one or split the build.
Monkey-patching still works. Expected. Third-party libraries remain Python source, so runtime patching of their behavior is still possible. Py2Native protects your proprietary modules — it doesn’t sandbox the entire interpreter.
LGPL compliance. If you link LGPL libraries, they must remain replaceable by the end user. Keep your distribution layout compliant.
__main__.py conflicts in library mode. Your own __main__.py is excluded from sources because the wheel builder generates one. Move your entry logic into a regular module and call it from there.
FAQ
Q: Do I need to know Cython to use Py2Native?
A: No. Py2Native is a zero-config compiler: you write plain Python, and it generates the necessary Cython code (bootstrap.pyx) and compiles it to a native binary behind the scenes. You never write .pyx or .pxd files yourself.
Q: Can I still use my favorite third-party libraries? A: Yes. Third-party Python libraries are left unmodified and work as-is with minimal or no configuration, unlike many other code-protection tools that require extensive build configuration.
Q: How do I protect my code with a license key?
A: Use the Pro plugin. Include a .pxd file and call _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT") in your code. The plugin bakes the signature verification code and public key into the executable, giving you maximum security and flexibility.
Q: What platforms are supported? A: Py2Native supports Windows 8+, manylinux2014/musl Linux, and macOS on x86-64 or ARM64 CPUs, with CPython 3.11–3.15 (including free-threaded 3.14t & 3.15t). A platform C compiler/linker is required.
Conclusion
The mental model to keep is straightforward: Cython is the engine, Py2Native is the steering wheel and pedals. You write plain Python, run one uv run py2native build command, and get a native binary, a wheel, or a shared library. Add --library for importable packages. Add the Pro plugin when you need a signed JWT license gate compiled into the executable rather than checked in patchable Python.
Nothing in that workflow requires you to learn Cython’s type syntax, maintain a setup.py, or hand-write .pxd files — except the single optional declaration the Pro plugin needs to wire in license verification.
Start with the core compiler, get a build green on a small project, then expand the glob. Full CLI, runtime API, and build artifact documentation is available in the llms-full.txt reference.
Next step: run uv run py2native build main.py *.py --wheel dist/ on your current project and inspect the wheel. If you’re choosing between the MIT core and the commercial tier, Open Source Python Compiler for Commercial Software: MIT Core covers exactly what you get for free — and where the Pro plugin picks up.
Related posts
- Open Source Python Compiler for Commercial Software: MIT Core
- Ship Compiled Python Wheels Automatically on Every Release
- How to Protect Python Code in Docker Container Deployments