Automate Python Builds for Windows, Linux, and macOS
If you ship a proprietary Python application, you already know the ritual. You build on Windows, copy the .pyd files to a share. You SSH into a Linux box, rebuild, and hope GCC flags match. You borrow a Mac to produce a .dylib nobody has tested. Then you do it again next release, by hand, and again after that.
Learning to automate Python builds for multiple platforms is the difference between shipping weekly and shipping when someone remembers the steps. This article walks through a one-time setup and a CI pipeline that turns your plain Python source into native binaries for Windows, Linux, and macOS — using Py2Native, which compiles your custom code to machine code with zero configuration and uses uv for everything it installs.
This is not a Cython tutorial. The classic path — writing .pyx/.pxd files, running cythonize yourself, gluing a C extension together with distutils — is the hard way. Py2Native uses Cython under the hood but hides it completely. If you want that comparison in depth, read Cython Alternatives for Protecting Python Code: Hard Way vs Zero-Config. Here, we stay on automation.
One-Time Setup: Prepare Your Project and CI Environment
The setup cost is paid once. After that, every release is a tag push.
1. Add Py2Native with uv.
uv add py2native
Do not reach for pip — uv is the supported package manager here, and it is also what Py2Native uses for embedded deployments.
2. Make sure your project has the shape Py2Native expects.
A pyproject.toml and a main.py entry point. Py2Native globs and compiles all .py files in your source tree, so you do not need a manifest of files to protect — everything is picked up.
3. (Optional, Pro only) Add the Pro plugin and generate a keypair.
uv add py2nativepro
uv run py2native keygen private.pem public.pem
The Community core is MIT-licensed and covers full compilation, uv embedding, and cross-platform builds. The Pro plugin is proprietary and adds JWT license verification, string compression, and the keygen / sign / show commands.
4. Provision runners for each target OS.
Py2Native does not cross-compile for you. Each platform needs its native toolchain:
- Windows — MSVC, Windows 8 or later, output named
.pyd - Linux — GCC, manylinux2014 or musl, output named
.so - macOS — Clang, output as a Mach-O
.dylib
Every runner also needs internet access, because Py2Native downloads Python distributions and libraries during the build. Supported runtime: CPython 3.11–3.15, including the free-threaded 3.14t and 3.15t builds, on x86-64 or ARM64.
5. (Pro only, advanced) Add a .pxd file if compiled code must call license verification.
Covered in its own section below — for most Python-level checks you will not need it.
The Automated Pipeline: Build Once, Ship Everywhere
The command that does the work is the same on every OS:
uv run py2native build main.py *.py --wheel dist/
That single invocation drives the whole pipeline. Behind the scenes the orchestrator globs your sources under --base, dispatches to platform and Pro plugins, generates a bootstrap.pyx from templates, runs Cython (--embed --shared --embed-modules) to produce C, compiles every .c file with the platform’s flags, links a shared object or an executable, and — because of --wheel — packages a PEP 427 wheel containing the compiled artifact.
Variants you will use depending on what you ship:
# Shared library (.pyd/.so/.dylib) instead of a standalone executable
uv run py2native build main.py *.py --library --wheel dist/
# A uv-managed deployment directory instead of a wheel
uv run py2native build main.py *.py --embed deploy/
# Non-standard source layout
uv run py2native build main.py *.py --base src/ --wheel dist/
# Windows GUI app: suppress the console window
uv run py2native build main.py *.py --no-console
# Pro: bake license verification and the public key into the binary
uv run py2native build main.py *.py --license license.dat --public public.pem --wheel dist/
In --library mode the wheel contains a single compiled shared library holding all your modules, plus two generated files that are both required: __init__.py, a meta-path finder that redirects import mypkg.submodule into the native library, and __main__.py, which makes python -m mypkg work. You never write those — the wheel builder emits them. (__init__.py in your own source tree is skipped during compilation, and your __main__.py is excluded from sources in library mode precisely because it is generated.)
A GitHub Actions matrix
This is the whole pipeline. Three runners, one build command, three artifacts.
name: native-build
on:
push:
tags: ["v*"]
jobs:
build:
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Install dependencies
run: uv sync
- name: Build native wheel
run: uv run py2native build main.py *.py --wheel dist/
- name: Smoke test
run: uv run python smoketest.py
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: wheel-${{ matrix.os }}
path: dist/*.whl
Swap the last two steps for --embed deploy/ if you deploy a directory rather than a wheel. Either way, the matrix handles the platform differences: Windows gets MSVC flags and PE linking, Linux gets ELF linking with automatic auditwheel repair, macOS gets Mach-O linking.
Integrating License Verification in Your Code (Pro Plugin)
The Pro plugin is where automation meets enforcement: you can build a protected binary and require a signed license to run it.
Generate the keypair once:
uv run py2native keygen private.pem public.pem
Sign a payload into a license file:
uv run py2native sign --private private.pem payload.json license.dat
uv run py2native show --public public.pem license.dat
show prints the JWT claims and can verify the signature against the public key — useful for support tickets.
Then verify at runtime, in ordinary Python:
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
If you need to call it from compiled code rather than from a plain Python module, add a small .pxd file declaring the interface:
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
and import it:
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
The plugin bakes the signature verification code and the public key into the executable. Only the public key ships; the elliptic-curve verification is handled by compiled code inside the product, with no third-party libraries involved. That is the maximum-security, maximum-flexibility configuration: you decide at runtime which features to gate, and an attacker cannot simply swap out a Python-level check.
For the broader pattern of runtime license checks, see Checking a Signed License Key Inside a Python Application.
Monitoring and Failure Handling in CI
Automation without visibility just fails faster. A few things to watch:
- Capture logs per platform. Py2Native emits detailed compiler and linker messages. Keep them as build artifacts — when a Windows link fails, the MSVC output is the only useful signal you will get.
- Know the usual suspects. Missing C compiler on the runner; an unsupported CPython version (the floor is 3.11, the ceiling 3.15); duplicate module names across source files. The last one is the most annoying, because it only appears once two files collide in the namespace.
- Check wheel tags on Linux.
auditwheelrepair is applied automatically to meet manylinux compliance, so confirm the resulting tag matches the platform you intend to support before publishing. - Run a smoke test after every build. A build that links is not a build that runs. A short
smoketest.pyexecuted post-build catches import-path and entry-point mistakes in seconds. - Notify on failure. Wire your CI’s Slack or email integration to the matrix job. With
fail-fast: falseyou get results for all three OSes in one run instead of discovering the macOS breakage on release day.
Best Practices for Cross-Platform Automation
- Keep source file names unique. No modules may share a name across source files. This is a hard constraint of the compilation model.
- Pin the Python version in CI. Match your target runtime exactly — 3.11 through 3.15, including free-threaded builds if you use them.
- Use
--basefor non-standard layouts instead of moving files around for the build system’s convenience. - Do not try to compile dependencies. Third-party libraries stay as Python source and work as-is with minimal or no configuration. Only your custom code becomes machine code, which is precisely what keeps cross-platform automation sane.
- Keep Py2Native,
cython,setuptools, anduvcurrent. The current floors arecython >= 3.2.4,setuptools >= 82.0.1, anduv >= 0.11.8. - Plan for what stays readable. Monkey-patching is still possible because third-party libraries remain Python source, and LGPL libraries stay replaceable by the end user for compliance. Design your architecture around that rather than fighting it.
FAQ
Do I need to write Cython code or C extensions to use Py2Native?
No. Py2Native compiles plain Python code directly. It uses Cython under the hood but hides it completely — you write normal Python, and Py2Native handles the compilation to native machine code without any .pyx or .pxd files (unless you need to call Pro license verification from compiled code, in which case you add a small .pxd file).
Can I build for Windows, Linux, and macOS from a single machine?
No, you need a build environment for each target platform. The recommended approach is to use a CI matrix (for example GitHub Actions) with runners for each OS. Each runner must have the appropriate C compiler — MSVC, GCC, or Clang — and internet access for Python downloads.
How do I integrate license key verification into my compiled Python app?
Use the Pro plugin. Generate a keypair with uv run py2native keygen private.pem public.pem, sign a JWT with uv run py2native sign, and then in your code call _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT"). The plugin bakes the verification code and the public key into the executable. You can also import it from compiled code with from _p2n_bootstrap cimport _runtime_verify_es256_jwt using a .pxd file.
What happens to third-party libraries when I compile my Python code?
Third-party libraries remain as Python source and work as-is with minimal or no configuration. Only your custom Python code is compiled to native machine code. You don’t need to rebuild or modify dependencies, which is what makes cross-platform automation straightforward.
Conclusion: Automate and Protect Your Python Builds
Manual per-platform builds are slow, error-prone, and invisible until something breaks in the middle of a release. A CI matrix plus one build command eliminates the repetition and gives you consistent, protected binaries for all three targets.
Start with the MIT-licensed core:
uv run py2native build main.py *.py --wheel dist/
Add the Pro plugin when you need to enforce signed license keys in the field — --license license.dat --public public.pem bakes verification into the executable, with only the public key shipped.
Related posts
- Is Compiled Python Really Harder to Reverse Engineer?
- Python Code Protection Tool Pricing: Open Source vs Commercial
- Cython Alternatives for Protecting Python Code: Hard Way vs Zero-Config