Python Code Protection for Commercial Software Vendors
Mira runs a four-person software company. Her product is a freight-reconciliation tool that took two years to build, and it’s written in Python — 40,000 lines of it, plus a dozen third-party libraries. Last quarter she closed her first enterprise deal, and during onboarding the customer’s IT team sent back a screenshot: they had opened the install directory and found her .py files sitting there in plain text.
Nothing was hacked. That’s just how Python ships.
Mira’s options at that point were the ones every small vendor faces: ask customers to sign a license agreement and hope, rewrite the core in C++ (a year of work she doesn’t have), or find a way to ship the same Python she already has — but as machine code instead of readable text. This article is about that third option, and specifically about what python code protection for commercial software vendors looks like as an engineering decision rather than a legal one.
If you’re shipping proprietary Python to paying customers, the rest of this is a playbook you can run this week.
The Vendor’s Dilemma: Shipping Python Without Shipping Source
Python’s openness is an asset during development and a liability at distribution time. Every customer who receives your application receives:
- Your module structure, which is a free architecture diagram.
- Your business logic — pricing rules, matching algorithms, scoring heuristics.
- Your comments, including the ones explaining why the algorithm works.
- Your internal API surface, ripe for cloning.
For a solo founder or a small agency, this is not abstract. The people most likely to read your source are exactly the people most likely to compete with you: a customer who thinks “we could build this in-house,” or a contractor who was given a copy of the install directory.
The business stakes are concrete:
- Licensing revenue leaks. If the code is readable, a per-seat license is a suggestion. Anything that can be copied can be run without you.
- Reverse engineering is cheap. Someone with a decompiler and an afternoon can lift a feature set.
- Due diligence gets awkward. Enterprise buyers increasingly ask what happens to your source when the product is installed on their machines.
What vendors need is a build step, not a rewrite. You keep writing Python — because that’s why the product exists and shipped on time — and the build produces a native artifact. That’s the entire thesis behind Py2Native: it compiles your custom Python modules into native machine code, hides Cython completely behind a single CLI command, and leaves your third-party dependencies untouched.
Why Commercial Vendors Can’t Just Obfuscate or Freeze
The instinctive first move is PyInstaller or an obfuscator. Both are fine for internal tooling. Neither clears the bar for a commercial product.
Freezing produces a bundle, not a barrier. A freezer’s job is packaging: it collects your modules and the interpreter into one file. Your code is still bytecode inside that file, and bytecode decompiles. The bundle raises the effort from “open the folder” to “extract the folder,” which is a difference of minutes.
Obfuscation renames things but keeps the logic. Renaming symbols and encrypting string literals makes output harder to read. It doesn’t change what the code does or how many instructions it has. A determined reader rebuilds the mapping from behavior, and the string table usually ships alongside the obfuscated code anyway.
There’s a second, quieter problem that trips up small vendors more often than reverse engineering does: third-party libraries. Your product almost certainly depends on a stack of them, and many protection approaches force you to configure each dependency individually — exclusion lists, hidden imports, per-library build flags. That’s maintenance debt that grows every time you bump a dependency, and it’s the reason these pipelines end up disabled six months after they’re added.
And a third: cross-platform builds. If your customers run Windows, Linux, and macOS, you need three working pipelines and three sets of flags. Anything that makes that harder is a non-starter for a team of four.
Py2Native’s answer to all three is the same: only your modules are compiled; third-party libraries stay as Python source and work as-is with minimal or no configuration; and the platform differences are handled by platform plugins you don’t have to think about. For a deeper comparison of compilation strategies, see Nuitka vs Cython Compilation: Which Protects Python Source Better? — this section is about the decision, not the mechanism.
A Practical Playbook for Protecting Your Commercial Python Product
Here’s the sequence, in the order you should run it. Everything uses uv.
Step 1: Compile your custom modules
Add Py2Native to the project and build. The first argument is your main module; the rest are the sources to compile.
uv add py2native
uv run py2native build main.py src/*.py app/*.py
That produces a native executable. If you’d rather ship a shared library that your existing Python entry point loads, add --library:
uv run py2native build main.py src/*.py --library
Under the hood this globs your sources, generates a bootstrap module, transpiles to C, compiles with your platform’s toolchain, and links against the CPython shared library. You don’t write .pyx files, you don’t run cythonize, and you don’t touch C. If you’ve ever tried compiling plain Python to native code without rewriting in C, this is the shortcut.
If you’re shipping a GUI app on Windows and don’t want a console window attached, add --no-console.
Step 2: Choose your distribution format
Two output modes matter for vendors:
# uv-managed deployment directory
uv run py2native build main.py src/*.py --embed dist/app
# PEP 427 wheel containing the compiled .pyd/.so
uv run py2native build main.py src/*.py --wheel dist/wheels --library
--embed gives you a self-contained directory with a uv-managed environment and your compiled binary — the thing you zip and hand to a customer. --wheel gives you a wheel with the compiled artifact inside, which is what you want if your product ships through an internal package index or a Python-based installer.
Use --base <dir> if your glob patterns need to be resolved from a different root than the current directory.
Step 3: Get library mode right
In --library mode the wheel contains three things worth understanding:
- A single compiled shared library (
.pydon Windows,.soon Linux,.dylibon macOS) containing all your modules. __init__.py— a meta-path finder that redirectsimport mypkg.submoduleinto the shared library.__main__.py— makespython -m mypkgwork.
Both Python files are required. __init__.py is the bridge between Python’s import system and the native library; __main__.py is the runnable entry point. Py2Native generates __main__.py for you and excludes it from the source list, and it skips your __init__.py during compilation because it’s a namespace marker, not compilable code. Don’t “fix” either of those behaviors — they’re load-bearing.
Step 4: Add license verification with the Pro plugin
The open-source compiler protects your code. The Pro plugin enforces who is allowed to run it — by baking an ES256 JWT signature check and the public key directly into the executable.
uv add py2nativepro
# one-time: generate an EC P-256 keypair
uv run py2native keygen private.pem public.pem
# per customer: sign a JWT payload
uv run py2native sign --private private.pem payload.json license.dat
# inspect (and optionally verify) the claims
uv run py2native show --public public.pem license.dat
Then build with the license gate and the public key:
uv run py2native build main.py src/*.py --license license.dat --public public.pem --embed dist/app
To call the verification logic from your own code, include a .pxd file in your source tree declaring the hook that the plugin bakes in:
# _p2n_bootstrap.pxd
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
and call it from the one module that gates your application:
# license_check.pyx
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
# _runtime_verify_es256_jwt(token, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
def check_license(licenseString):
license = _runtime_verify_es256_jwt(licenseString, expected_iss="RSJ Software GmbH", expected_aud="TimestampGIT")
return license
This is the only place Py2Native exposes anything resembling Cython syntax, and it exists because the verification call is a compile-time binding to compiled code. Everything else in your product stays plain Python. The important part is what ends up in the binary: the verifier and the public key. The private key never leaves your machine, and the elliptic-curve verification is compiled into your software — no third-party crypto library to swap out.
For a fuller treatment of the signing flow, see How to Sign and Verify JWT License Keys for Python Apps.
Step 5: Smoke-test the artifact, not the source
Never ship a build you haven’t run. A minimal smoke test:
uv run py2native build main.py src/*.py --embed dist/app
./dist/app/<your-entrypoint> --help
Then confirm that your own modules are no longer exposed:
find dist/app -name "*.py" -path "*yoursourcepackage*"
You should find nothing. Third-party libraries will still be present as .py files — that’s expected and intentional, covered in the next section.
Automate both checks in CI before you tag a release; the GitHub Actions walkthrough shows the wiring.
What to Avoid When Protecting Commercial Python Code
- Don’t hand-roll a Cython pipeline. Writing
.pyxfiles, managingcythonizeinvocations, and maintaining C extension builds across three platforms is weeks of work you’ll redo every time Python bumps a version. Py2Native is that pipeline, pre-built. - Don’t adopt tools that require you to rewrite your code. Proprietary syntax, decorators everywhere, or a “supported subset” of Python means your codebase now has a fork that only one tool understands. You should keep writing plain Python.
- Don’t neglect license compliance. If you link LGPL libraries, end users must be able to replace them. Py2Native respects this by leaving third-party libraries as Python source — which is also why they keep working without configuration.
- Don’t assume obfuscation is a security boundary. It’s a speed bump. Native compilation is a materially stronger barrier: there’s no bytecode to decompile, only machine code.
- Don’t forget the duplicate-name rule. Py2Native can’t handle two source files with the same module name. Flatten or rename before you build; the error is much cheaper to fix at design time.
How Py2Native Fits the Commercial Vendor’s Workflow
The compiler is a CLI, so it drops into whatever you already use — a Makefile, a shell script, a GitHub Actions job. uv run py2native build ... is the whole interface.
Runtime support covers CPython 3.11 through 3.15, including the free-threaded 3.14t and 3.15t builds, on Windows 8+, manylinux2014/musl Linux, and macOS, for x86-64 and ARM64. Requirements are a normal platform toolchain (MSVC, GCC, or Clang) and internet access on first build so Python distributions and libraries can be downloaded.
The plugin architecture is what keeps platform complexity out of your build script. A pluginManager loads modules by name and dispatches through lifecycle hooks — extendBuildParser(), extendBuild(), extendCompiler(), extendTemplate(), getCompileLibArgs(), getLinkerArgs(), getExecutableName(), getRuntimeLibraries(), auditWheel(). The platform plugins (Windows, Linux, macOS) supply flags, naming, and — on Linux — auditwheel repair for manylinux compliance. The Pro plugin hooks in the same way to add JWT verification and string compression.
The core compiler is MIT-licensed and open source. The Pro plugin is proprietary and adds the license enforcement layer. That split matters for vendors: you can audit and vendor the compiler, and pay only for the part that protects revenue.
One honest caveat: monkey-patching remains possible, because third-party libraries are still Python source. That’s the compliance trade-off, and it’s the right one — your code is protected, and your dependencies stay replaceable.
FAQ
Can I protect my Python code without rewriting it in C or Cython?
Yes. Py2Native compiles plain Python directly to native machine code using Cython under the hood, but you never write Cython syntax or manage .pyx files. You just run uv run py2native build on your existing Python sources.
How do I enforce license keys in my compiled Python application?
Use the Pro plugin. Generate an EC P-256 keypair with uv run py2native keygen, sign a JWT payload with uv run py2native sign, and embed verification in your code using _runtime_verify_es256_jwt from the _p2n_bootstrap module. The public key and verification logic are compiled into the executable.
Will my third-party dependencies still work after compilation?
Yes. Py2Native leaves third-party Python libraries unmodified, so they work as-is with minimal or no configuration. Only your custom modules are compiled to native code.
What platforms does Py2Native support for commercial distribution?
Py2Native supports Windows 8+, manylinux2014/musl Linux, and macOS on x86-64 or ARM64 CPUs, with CPython 3.11–3.15 including free-threaded variants.
The Bottom Line for Vendors
Mira’s fix wasn’t a rewrite or a legal letter. It was a build step: uv run py2native build main.py src/*.py instead of a folder of .py files, plus --license so the binary checks a signed JWT before it runs. Her customers get a native application. Her competitors get machine code.
If you’re selling Python software, the source code is your product. Ship it compiled.
Start with uv add py2native, run one build against your main module, and diff what your customers receive. Then add the Pro plugin when you’re ready to tie the binary to a license key.
Related posts
- Nuitka vs Cython Compilation: Which Protects Python Source Better?
- Automate Python Code Protection in GitHub Actions with uv
- Compile Plain Python to Native Code Without Rewriting in C