Py2Native Python to native compilerPython-zu-Native-Compiler

← All posts

2026-10-15

How Py2Native's --embed Builds a Portable Deployment Folder

How Py2Native's --embed Builds a Portable Deployment Folder
python compiler cython code protection open source

How Py2Native’s --embed Builds a Portable Deployment Folder

You compiled your Python to native code. The compile itself took one command. Then you spent the rest of the afternoon doing the part nobody enjoys: copying python3xx.dll next to the executable, hunting down the .so files the linker didn’t pull in, writing a run.sh and a run.bat that disagree with each other, and hoping the target machine has a compatible CPython installed.

That manual assembly step is exactly what --embed deletes. One flag on the Py2Native build command, and you get a self-contained, uv-managed deployment directory you can zip, copy, and run.

This article is about the automation angle specifically: the one-time setup, what the pipeline does on every run, how to tell when the embed step went wrong, and how to keep the shipped folder lean. For the compilation fundamentals, see Python Native Compiler Alternatives and What Does Cython Actually Do to Your Python Code?.

One-Time Setup: Install Py2Native and Prepare Your Project

The setup is short, and you only do it once per machine or per CI image.

1. Install uv. Py2Native uses uv >= 0.11.8 as its package manager, and every command starts with uv run py2native.

2. Install Py2Native. Inside a project:

uv add py2native

Or as a global tool, if you build many projects:

uv tool install py2native

No pip, no virtualenv juggling — uv manages the environment.

3. Give your project a plain Python layout. A main module plus your source files. That’s it. No .pyx, no .pxd, no setup.py with Extension() blocks, no manual cythonize step. If you’ve ever protected code “the hard way” with hand-written Cython and a custom setup.py, this is the part that feels suspicious at first: you really do just write Python.

4. Check the prerequisites:

  • CPython 3.11–3.15, including the free-threaded 3.14t and 3.15t builds
  • A platform C compiler and linker — MSVC on Windows, GCC on Linux, Clang on macOS
  • Windows 8+, manylinux2014/musl Linux, or macOS
  • x86-64 or ARM64
  • Internet access, because the embed step downloads Python distributions and libraries

5. Optional — the Pro plugin. If you need license verification, add py2nativepro and generate a keypair once:

uv run py2native keygen private.pem public.pem

That produces an EC P-256 keypair. Keep private.pem off your build servers and out of your repository forever. More on that below.

The Automated Pipeline: One Command to a Portable Folder

Here’s the whole build:

uv run py2native build main.py *.py --embed dist/

That single command runs the full pipeline:

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

Step 7 is the one we care about. embed.py doesn’t just dump your binary somewhere — it builds a real directory that carries its own Python runtime and its own dependency set, resolved and downloaded by uv.

The layout is platform-dependent, but the shape is consistent:

dist/
├── myapp.exe / myapp      # your compiled native executable
├── python/                # CPython runtime the deployment uses
├── pyvenv.cfg             # marks the directory as a Python environment
├── lib/                   # third-party dependencies resolved by uv
└── uv metadata            # pins exactly what was installed

Copy that folder to a machine with no Python installed, run the executable, and it works. That’s the whole point: no system Python, no activate step, no “which python am I using?” debugging on a customer’s box.

Optional flags you’ll probably combine with --embed

# Also emit a PEP 427 wheel alongside the embedded folder
uv run py2native build main.py *.py --embed dist/ --wheel dist/wheels

# Shared library mode instead of an executable
uv run py2native build main.py *.py --library --embed dist/

# GUI app: suppress the console window on Windows
uv run py2native build main.py *.py --embed dist/ --no-console

# Limit which sources get globbed
uv run py2native build main.py *.py --embed dist/ --base src/

# Pro: require a valid signed license at build time
uv run py2native build main.py *.py --embed dist/ \
  --license license.dat --public public.pem

A note on --library: it produces a shared library (.pyd/.so/.dylib) instead of an executable, and the wheel it generates contains the compiled library plus two generated files that are both required:

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

__init__.py bridges Python’s import system to the native library; __main__.py gives you the runnable entry point. If you want the deeper version of that mode, read How to Compile Python to a Shared Library Instead of an Executable.

Monitoring and Failure Handling in the Embed Step

Most build failures are boring, and they’re almost never in the Cython step. Here’s what to check, roughly in order of frequency.

Missing platform compiler. If getCompiler() can’t find a toolchain, the build dies before embed ever runs. Confirm MSVC, GCC, or Clang is installed and on PATH for the user running uv run py2native.

Dependency resolution errors. The embed step downloads Python distributions and libraries, so it needs network access. Behind a proxy or on an air-gapped runner, that’s your failure point on every build.

Name collisions. Py2Native requires that no two source files share a module name. Two files both called utils.py in different directories will break the build. Rename or restructure. (Note that __init__.py is skipped during compilation anyway — it’s a namespace marker, not code — and in library mode __main__.py is excluded from sources because the wheel builder generates it.)

Duplicate or stale outputs. Building into a dist/ that already contains an old embedded environment is a classic source of “it works on my machine” confusion — especially in CI with a cached workspace. Wipe the directory before rebuilding:

rm -rf dist/ && uv run py2native build main.py *.py --embed dist/

License verification failures (Pro only). If --license is in play and the build rejects the token, inspect the claims rather than guessing:

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

show prints the JWT claims and, with --public, verifies the signature against the public key. If the signature doesn’t validate, the token was signed with a different private key — or it’s simply the wrong file.

Wiring the verification into your own code

The Pro plugin bakes the ES256 signature verification code and the public key into the executable. You declare the runtime function once in a .pxd file:

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

And call it from your compiled code with the license string you collected at runtime:

# licensing.py — compiled as part of the build
from _p2n_bootstrap cimport _runtime_verify_es256_jwt

def check_license(licenseString: str):
    license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
    return license

The verification is handled in compiled code shipped with the software — no third-party crypto libraries in the chain — and only the public key is stored in the executable. There’s a full walkthrough in Verifying a Signed JWT License Inside a Compiled Python App.

Verify the artifact, not the build log. The only real test is running the embedded folder somewhere clean:

docker run --rm -v "$PWD/dist:/app" debian:bookworm-slim /app/myapp

If it runs in a container with no Python, your deployment folder is what you think it is.

Best Practices for Shipping Embedded Deployments

  • Keep the folder minimal. Use --base to narrow which sources are globbed, and don’t leave test fixtures, sample data, or notebooks in the build path. Every file you glob is a file you compile and ship.
  • Never ship the private key. For license-protected apps, only the public key belongs in the executable — the plugin handles that. The private key lives on the machine that runs uv run py2native sign and nowhere else.
  • Test on target platforms early. Windows 8+, manylinux2014/musl Linux, macOS, on x86-64 and ARM64. Discovering an ARM64 problem the day before release is a bad day.
  • Add --wheel if you also distribute as a package. The wheel carries the compiled .pyd/.so plus the import shims, so Python consumers get a normal install path.
  • Make the build one CI step. The whole reason --embed exists is that this used to be a pile of packaging scripts. Keep it that way:
# .github/workflows/release.yml
- name: Build embedded deployment
  run: uv run py2native build main.py "*.py" --embed dist/ --wheel dist/wheels

Then upload dist/ as a release artifact. No custom shell scripts, no cp of runtime DLLs, no per-platform packaging logic to maintain.

FAQ

What exactly does --embed put in the deployment folder?

The --embed flag creates a self-contained directory that includes your compiled native executable, a uv-managed Python environment (with the necessary CPython runtime), and all required third-party dependencies. This folder can be copied to a target machine and run without a system-wide Python installation.

Can I use --embed with --library or --wheel?

Yes. --embed can be combined with --wheel to also produce a PEP 427 wheel containing the compiled .pyd/.so and import shims. --library mode creates a shared library instead of an executable; the embed step then packages that library with the runtime. Both flags are independent and can be used together.

How does --embed handle third-party dependencies?

Py2Native leaves third-party Python libraries unmodified and works with them as-is. During the embed step, it uses uv to resolve and download the required packages into the deployment folder, ensuring they are available at runtime without manual configuration.

Is the embedded folder truly portable across machines?

Yes, as long as the target machine meets the platform requirements (Windows 8+, manylinux2014/musl Linux, or macOS) and architecture (x86-64 or ARM64). The folder includes the Python runtime and dependencies, so no separate Python installation is needed. However, system-level libraries like glibc may still be required on Linux.

Conclusion: Automate Deployment, Ship with Confidence

Compiling Python to native code solves the code-protection problem. Packaging it is the problem that comes right after, and it’s the one that quietly eats a day per release. --embed collapses that chore into the last flag of a command you were already running: the compiled executable, a uv-managed runtime, and the resolved dependencies, all in one folder you can hand to a customer or drop into a container.

That’s the zero-config promise in practice — you write plain Python, you get a native binary with its runtime embedded, and you never touch a pyvenv.cfg by hand.

Try it in your project:

uv run py2native build main.py *.py --embed dist/

If you also need to gate the app behind a signed license, add the Pro plugin, generate your keypair with uv run py2native keygen, and pass --license on the build. The verification logic and public key get baked into the binary; the private key stays with you.

Related posts

EU label: AI-generated content