Py2Native Python to native compilerPython-zu-Native-Compiler

← All posts

2026-10-24

How to Ship a Python App Without Exposing Your Source Code

How to Ship a Python App Without Exposing Your Source Code
python compiler cython code protection open source

How to Ship a Python App Without Exposing Your Source Code

Your app is proprietary, but the artifact you ship is a folder of readable .py files. Anyone with the zip can open pricing.py, read your business rules, and copy them. That’s the shipping problem — not “how do I run Python on a customer machine,” but “how do I hand over a working application without handing over the source that makes it valuable.”

The task in this article is concrete: compile your custom Python code into a native machine code binary, so end users get a runnable app instead of your source.

You can do that the hard way — write .pyx files, hand-author C extension modules, and wire up a cythonize step in your build. That path works, but it turns a packaging problem into a C toolchain project. This article takes the zero-config route with Py2Native: you write plain Python, and Py2Native compiles it to a native binary. Cython is used under the hood but stays completely hidden — no .pyx/.pxd authoring, no manual cythonize, no C to write. If you’re weighing the two approaches, our companion piece Cython Alternatives for Protecting Python Code: Hard Way vs Zero-Config covers that trade-off in depth.

By the end you’ll have: a compiled binary (or a compiled importable library), a verification procedure to prove your source isn’t in the output, and — optionally — signed JWT license verification baked into the executable via the Pro plugin.

Prerequisites: What You Need Before You Compile

  • 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. It must be on PATH.
  • A supported OS/CPU: Windows 8+, manylinux2014 or musl Linux, or macOS — on x86-64 or ARM64.
  • uv installed, and the open-source py2native package added with uv (not pip).
  • Internet access during the build, so Python distributions and libraries can be downloaded.

Set up the project-local install once:

uv init myapp-build
cd myapp-build
uv add py2native

If you want signed license verification and string compression, add the proprietary Pro plugin alongside it:

uv add py2nativepro

Every command in this article runs through uv run py2native ....

Step 1: Prepare Your Python Project for Compilation

Py2Native compiles the modules you point it at, so start by confirming what “your code” actually is:

  • Organize your custom code into modules under a source root. Use --base <dir> if that root isn’t your current directory.
  • Respect the naming constraint: no modules with identical names across source files. A utils.py in two different packages will collide at link time — rename one.
  • Don’t rely on __init__.py being compiled. It’s a namespace marker, not compilable code, so it’s skipped during compilation.
  • In library mode, __main__.py is excluded from your sources — the wheel builder generates it.
  • Leave third-party libraries alone. They stay unmodified and work as-is with minimal or no configuration. You’re only compiling your own code.

Finally, decide your output mode: an executable (the default) if you ship a standalone app, or library mode (--library) if you ship an importable package.

Step 2: Compile Plain Python to a Native Binary with Py2Native

The command shape is:

uv run py2native build main.py *.py [--library] [--embed <dir>] [--wheel <dir>]

A realistic standalone build looks like this:

uv run py2native build app/main.py "app/**/*.py" --base app

A GUI app that shouldn’t pop a console window:

uv run py2native build app/main.py "app/**/*.py" --no-console

Here’s what happens after you press Enter:

  1. Glob sources — build.py expands your glob patterns under --base.
  2. Plugin dispatch — pluginManager.extendBuild(args) runs, including any Pro license checks.
  3. Cython → C — compiler.py generates a bootstrap.pyx from templates.py, invokes Cython (--embed --shared --embed-modules), and transpiles every module to C.
  4. C → objects — getCompiler() compiles the generated .c files with your platform’s flags.
  5. Link — link_shared_object() for library mode, link_executable() for executable mode.
  6. Wheel (optional) — whl.py builds a PEP 427 wheel containing the compiled .pyd/.so.
  7. Embed (optional) — embed.py creates a uv-managed deployment directory.

That transpile-and-link machinery is why you don’t write Cython. You didn’t create a .pyx file, you didn’t run cythonize, and you didn’t touch a setup.py extension definition. Py2Native also manages a pristine Python venv internally for the compilation step, so your working environment stays clean.

Optional flags worth knowing:

  • --embed <dir> — produce a self-contained, uv-managed deployment directory.
  • --wheel <dir> — produce a PEP 427 wheel wrapping the compiled shared library.
  • --library — build a shared library instead of an executable.
  • --no-console — suppress the console window on GUI apps.
  • --base <dir> — set the source root for glob expansion.

Step 3: Choose Your Output Format (Executable, Library, or Wheel)

Pick the format based on how you actually deliver the software:

Executable mode (default). A native binary that runs your app directly. Best for desktop tools, internal services, or anything you hand over as “here’s the program.”

Library mode (uv run py2native build main.py *.py --library). Produces a shared library (.pyd/.so/.dylib) plus two generated files that are both required:

  • __init__.py — a meta-path finder that redirects import mypkg.submodule to the compiled shared library.
  • __main__.py — enables python -m mypkg.

The library contains all your modules in a single compiled shared object. Best when the consumer needs to import your package and call into it.

Wheel mode (--wheel <dir>). Packages the compiled .pyd/.so into a PEP 427 wheel so you can distribute it like any other installable. Best for PyPI-style or private-index distribution.

Embed mode (--embed <dir>). Creates a uv-managed deployment directory with the compiled artifacts and runtime. Best when you want a self-contained app directory rather than a single binary.

You can combine them. A common production command compiles and produces both deployment artifacts in one pass:

uv run py2native build app/main.py "app/**/*.py" --wheel dist/ --embed build/deploy

Step 4: Add License Verification with the Pro Plugin (Optional)

If you need to control who can run the binary, the py2nativepro plugin adds signed JWT license verification. Start by generating an EC P-256 keypair:

uv run py2native keygen private.pem public.pem

Sign a JWT payload with the private key:

uv run py2native sign --private private.pem payload.json license.dat

Inspect the claims — optionally verifying the signature against the public key:

uv run py2native show --public public.pem license.dat

Now wire the check into your code. This is the one place where you include a .pxd declaration alongside your Python, and it’s what gives you maximum security and flexibility. Declare the verification entry point the plugin exposes:

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

Then call it with your license string and the claims you expect, cimporting the declaration from the bootstrap module:

from _p2n_bootstrap cimport _runtime_verify_es256_jwt

license = _runtime_verify_es256_jwt(
    licenseString,
    expected_iss="RSJ Software GmbH",
    expected_aud="TimestampGIT",
)

Build with the license and public key, and the plugin bakes the signature-verification code and public key straight into the executable:

uv run py2native build app/main.py "app/**/*.py" \
  --license license.dat \
  --public public.pem

A few things worth knowing about how this protects you:

  • Only the public key is stored in the executable. The private key never leaves your signing machine.
  • Elliptic key verification is handled with compiled code inside the software — no third-party libraries to bundle or trust.
  • The --license flag on build requires a valid Pro license.

Step 5: Verify the Build and Confirm Source Is Not Exposed

Don’t take the compiler’s word for it. Run the artifact:

./dist/myapp          # executable mode
python -m mypkg       # library mode, using the generated __main__.py

Then inspect what actually landed in the output directory:

  • You should see compiled .pyd/.so/.dylib files for your custom modules.
  • You should not see readable .py source for those same modules.
  • In library mode, __init__.py and __main__.py should both be present — they’re required for imports and for python -m execution.
  • With --embed, the uv-managed deployment directory should contain the compiled artifacts and runtime.

Finally, check the imports resolve end-to-end, since the meta-path finder is doing real work:

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

You can automate all of the above with a smoke test, the way the repository’s smoketest.py does — build, import, and assert on the output in one script you run in CI.

Troubleshooting Common Issues

Build fails with “module not found” or duplicate symbols. You have modules sharing identical names across source files. Rename one — Py2Native can’t disambiguate them at link time.

Missing compiler. Install MSVC (Windows), GCC (Linux), or Clang (macOS) and confirm it’s on PATH before running the build.

Library mode import errors. Check that __init__.py and __main__.py are present in the wheel output, and that you didn’t pass your own __main__.py as a source — it’s generated by the wheel builder.

License verification fails. Confirm expected_iss and expected_aud match the claims in your JWT exactly, and that the public key used in the build corresponds to the private key that signed the license.

Wheel isn’t manylinux-compliant on Linux. The Linux platform plugin runs auditwheel repair for you — make sure auditwheel is available in the build environment.

FAQ

Do I need to write Cython or C extensions to protect my Python code?

No. Py2Native compiles plain Python directly. It uses Cython under the hood but hides it completely — you don’t write .pyx/.pxd files, run cythonize manually, or write C extensions. You just run uv run py2native build.

Can I still use third-party Python libraries in my compiled app?

Yes. Third-party libraries are left unmodified and work as-is with minimal or no configuration. Only your custom code is compiled to native machine code.

How do I add license key verification to my compiled Python app?

Use the Pro plugin (py2nativepro). Generate a keypair with uv run py2native keygen, sign a JWT with uv run py2native sign, then in your code call the verification logic via the required interface:

from _p2n_bootstrap cimport _runtime_verify_es256_jwt

cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)

license = _runtime_verify_es256_jwt(
    licenseString,
    expected_iss="RSJ Software GmbH",
    expected_aud="TimestampGIT",
)

Build with --license and --public; the plugin bakes the public key and verification code into the executable. You need to include the .pxd file and call the verification logic with the license key in your code.

What platforms and Python versions does Py2Native support?

CPython 3.11–3.15 (including free-threaded 3.14t and 3.15t) on Windows 8+, manylinux2014/musl Linux, or macOS, with x86-64 or ARM64 CPUs. You need a platform C compiler/linker (MSVC, GCC, or Clang) and internet access for downloads.

Conclusion: Ship Protected Python Without the Cython Headache

Shipping a Python app without exposing source code comes down to one move: compile your custom Python to a native binary. With Py2Native that means plain Python in, native binary out — no Cython syntax, no .pyx files, no manual cythonize step, no C extensions to maintain. The open-source core (MIT) handles the compilation; the Pro plugin layers on signed JWT license verification when you need to control who runs it.

If you want to go further, Is Compiled Python Really Harder to Reverse Engineer? digs into what compilation actually buys you, and Python Code Protection Tool Pricing: Open Source vs Commercial covers the cost side. Once your build works locally, Automate Python Builds for Windows, Linux, and macOS shows how to run it across all three platforms.

Start small: point Py2Native at one module and run uv run py2native build main.py *.py. Watch a readable .py file become a native binary — then scale the same command to your full app.

Related posts

EU label: AI-generated content