Py2Native Python to native compilerPython-zu-Native-Compiler

← All posts

2026-10-16

How to Ship a PEP 427 Wheel With Compiled Modules

How to Ship a PEP 427 Wheel With Compiled Modules
python compiler cython code protection open source

How to Ship a PEP 427 Wheel With Compiled Modules

A pure-Python wheel lies about nothing. Unzip mypkg-1.0.0-py3-none-any.whl and every .py file you spent months writing is sitting there in plain text — importable, readable, copy-pasteable. If your package is what you sell, that’s the whole business model in a zip archive.

The fix is a wheel whose modules are compiled extension modules: .pyd on Windows, .so on Linux and macOS. Same install command, same import statements, but the logic arrives as machine code instead of source.

This article walks the --wheel workflow in Py2Native end to end. By the end you’ll have a PEP 427 wheel that installs into a clean environment, supports python -m mypkg, and contains no readable source — all from plain Python you already wrote.

The hard way, for the record, is writing .pyx files yourself, wiring up cythonize, and maintaining a build backend per platform. Py2Native automates that entire pipeline; you never touch a setup.py full of compiler flags.

Why Ship a Wheel With Compiled Modules?

Three reasons, in the order they usually matter:

  • Source protection. Your proprietary logic ships as a native shared library. Nothing in the wheel is readable Python except the two small bridge files that Python’s import system requires.
  • Distribution. Wheels are the standard unit of Python packaging. Your users get the same uv pip install mypkg experience they already expect — no unpacking tarballs, no running an installer script.
  • Import behavior. Extension modules are loaded as native code rather than compiled to bytecode at first import, which keeps startup predictable for large packages.

Compare the two artifacts:

Pure-Python wheel Compiled wheel
Tag py3-none-any platform-tagged (e.g. cp311-cp311-manylinux_2_17_x86_64)
Contents .py source files one shared library + two bridge files
Readable by user Yes No
Built per platform No Yes

Py2Native is the zero-config tool that gets you the right column. You write plain Python — no Cython syntax, no .pyx/.pxd authoring, no manual cythonize step, no C extensions. Third-party libraries stay untouched and keep working.

One distinction before we start: this article is about --wheel, which produces a distributable archive. If you want a standalone deployment directory with an executable instead, that’s the --embed path — see How Py2Native’s --embed Builds a Portable Deployment Folder.

Prerequisites

  • CPython 3.11–3.15, including the free-threaded 3.14t and 3.15t builds.
  • A platform C compiler/linker: MSVC on Windows, GCC on Linux, Clang on macOS.
  • uv >= 0.11.8 — every command in this article goes through uv.
  • Py2Native, added to your project.
  • Your package source: a main module plus the supporting .py files. No Cython syntax required.
  • Supported targets: Windows 8+, manylinux2014/musl Linux, or macOS, on x86-64 or ARM64. The build downloads Python distributions and libraries, so it needs internet access.
uv init mypkg-project
cd mypkg-project
uv add py2native
uv run py2native --help

Step-by-Step: Building a Compiled Wheel

Step 1: Organize your source

Start from a flat, unambiguous layout:

mypkg-project/
├── main.py          # entry point
├── core.py          # your proprietary logic
├── submodule.py
├── __init__.py      # package marker
└── pyproject.toml

Two rules to respect up front:

  • No two source files may share a module name. Every module ends up inside one compiled shared library, so core.py in two directories will collide. Rename or restructure before building.
  • __init__.py is skipped during compilation. It’s a namespace marker, not compilable code.
  • __main__.py is excluded from sources in library mode. The wheel builder generates it for you.

Step 2: Run the build

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

What each part does:

  • main.py *.py — the main module plus the glob of sources to compile. Py2Native expands glob patterns under --base if you pass one.
  • --library — produce a shared library (.pyd/.so/.dylib) instead of an executable. This is what makes the output importable rather than runnable.
  • --wheel dist/ — write the PEP 427 wheel into dist/.

Under the hood, Py2Native generates a bootstrap.pyx from its templates, invokes Cython (--embed --shared --embed-modules), compiles every .pyx to C, compiles the C to objects using your platform’s flags, links a shared object, and finally packages the wheel. You never see any of it.

If your sources live in a subdirectory, point at it instead of flattening:

uv run py2native build main.py *.py --base src --wheel dist/ --library

Step 3: Understand what’s in the wheel

The wheel contains exactly three kinds of files:

  • One compiled shared library — contains all your modules.
  • __init__.py — a meta-path finder. It redirects import mypkg.submodule to the compiled library instead of looking for a .py file on disk.
  • __main__.py — the runnable entry point, enabling python -m mypkg.

Both Python files are required. Delete either one and the package stops importing or stops running. They’re small on purpose: they’re the bridge between CPython’s import machinery and your native code, not a place to put logic.

Step 4: (Optional) Add license verification with the Pro plugin

If you sell licenses rather than just protecting code, the py2nativepro plugin adds JWT license verification. This is the one place where you touch a Cython-facing file — a small .pxd that declares the runtime verification entry point:

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

Then import it and validate the license in your own module:

# licensing.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

The verification is ES256, handled entirely by compiled code in the product — no third-party crypto libraries, and only the public key ever ends up in the executable.

Step 5: (Optional) Build with --license and --public

Generate a keypair, sign a payload, and build with the license baked in:

uv run py2native keygen private.pem public.pem
uv run py2native sign --private private.pem '{"sub":"customer-42","exp":1893456000}' license.dat
uv run py2native show --public public.pem license.dat

Then rebuild the wheel so the public key and the verification code are embedded in the compiled binary:

uv run py2native build main.py *.py --wheel dist/ --library --license license.dat --public public.pem

--license requires a valid Pro license to be present at build time. py2nativepro is the proprietary plugin that ships keygen, sign, and show; add it alongside the open-source core:

uv add py2nativepro

Step 6: Inspect the wheel

Never ship a wheel you haven’t looked inside. On Linux, after auditwheel repair, the contents should look like this:

unzip -l dist/mypkg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.whl
Archive:  dist/mypkg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.whl
  Length      Name
---------  ------------------------------------------
  1214832  mypkg/_mypkg.cpython-311-x86_64-linux-gnu.so
      412  mypkg/__init__.py
       98  mypkg/__main__.py
      684  mypkg-1.0.0.dist-info/METADATA
     1092  mypkg-1.0.0.dist-info/RECORD

One .so, two Python files, no source. On Windows the extension is .pyd; on macOS it’s .so built from a Mach-O .dylib.

How to Confirm It Worked

Test in a virtual environment that has nothing else installed — this catches missing runtime dependencies that your dev environment was quietly providing.

uv venv .verify
source .verify/bin/activate        # Windows: .verify\Scripts\activate
uv pip install dist/mypkg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.whl

Check the entry point:

python -m mypkg --version

Check the meta-path finder by importing a submodule that was compiled, not copied:

python -c "import mypkg.submodule; print(mypkg.submodule.__file__)"

You should see the path of the shared library, not a .py path inside site-packages.

Check that no source shipped:

ls .verify/lib/python3.11/site-packages/mypkg/
# _mypkg.cpython-311-x86_64-linux-gnu.so  __init__.py  __main__.py

Windows equivalent is .verify\Lib\site-packages\mypkg\. If you see core.py or submodule.py in that listing, the build didn’t compile what you think it compiled.

Confirm it’s genuinely native code:

file .verify/lib/python3.11/site-packages/mypkg/_mypkg*.so
# ELF 64-bit LSB shared object, x86-64, dynamically linked

Troubleshooting Common Issues

Duplicate module names across source files. The most common failure. Since everything links into a single shared library, utils.py in two places is a hard conflict. Rename one, or restructure into distinct module names.

__init__.py or __main__.py missing from the wheel. Don’t remove them, and don’t overwrite them with your own versions. They’re generated by the wheel builder; __init__.py carries the import redirection and __main__.py carries the entry point. If either is absent from unzip -l, rebuild and inspect again.

Platform-specific linking errors. Usually a missing or unreachable compiler. Verify cl is on PATH for MSVC, gcc for Linux, or clang for macOS. Py2Native’s runtime.py locates the CPython shared library it needs to link against — if that lookup fails, it’s almost always a mismatched interpreter version.

License verification failures. Confirm the Pro plugin is installed (uv add py2nativepro), that the JWT was signed with the matching private key, and that the build actually received --license and --public. Use uv run py2native show --public public.pem license.dat to print the claims and verify the signature before blaming the runtime.

Wheel not manylinux-compliant. On Linux, auditwheel repair is applied automatically during the build. Check the platform tag in the filename — a proper result reads manylinux_2_17 or musllinux, not linux_x86_64.

FAQ

What is the difference between --wheel and --embed?

The --wheel flag creates a PEP 427 wheel containing a compiled shared library (.pyd/.so/.dylib) and two Python bridge files (__init__.py and __main__.py). The --embed flag creates a uv-managed deployment directory with an executable. Both protect your source code, but wheels are for distribution via package indexes, while embed is for standalone deployment folders.

Can I ship a wheel with compiled modules for multiple platforms?

Yes. Py2Native supports Windows, Linux (manylinux2014/musl), and macOS on x86-64 and ARM64. You build the wheel on each target platform (or in CI) to produce platform-specific wheels. The open-source core handles cross-platform compilation, and the Pro plugin adds license verification.

Do I need to write Cython or C extensions?

No. Py2Native uses Cython under the hood but hides it completely. You write plain Python — no .pyx/.pxd files, no manual cythonize step, no C extensions. The only exception is when using the Pro plugin for license verification, where you include a small .pxd file and call the provided verification function.

How does license verification work in a compiled wheel?

With the Pro plugin, you generate an EC P-256 keypair (uv run py2native keygen), sign a JWT payload (uv run py2native sign), and build with --license and --public. The plugin bakes the public key and signature verification code into the compiled binary. At runtime, your code calls _runtime_verify_es256_jwt(token, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT") to validate the license.

Conclusion

The workflow is four commands’ worth of work if you strip it down: organize your modules so names don’t collide, run uv run py2native build main.py *.py --wheel dist/ --library, unzip the result to verify it contains one shared library and two bridge files, then install it in a clean environment and run python -m mypkg. You wrote plain Python the entire time.

That’s the zero-config promise: no .pyx authoring, no cythonize step, no per-platform build backend to maintain, and third-party libraries that keep working untouched. What ships to your users is a standard, platform-tagged wheel that installs with the tooling they already use — it just doesn’t contain your source code.

If you need to gate that wheel behind signed licenses, the Pro plugin adds keygen, sign, show, and the --license/--public build flags, with the public key and ES256 verification compiled into the binary.

Try it on your own package:

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

Then unzip -l dist/*.whl and see for yourself what your users will get.

Related posts

EU label: AI-generated content