What Happens to Third-Party Libraries When You Compile Python?
You’ve got a proprietary Python service. You’ve decided to stop shipping readable .py files and compile it instead. Then you look at your pyproject.toml and the panic sets in: forty-three dependencies. numpy, pydantic, sqlalchemy, httpx, and something a contractor added in 2019 that nobody dares touch.
Do all of those get compiled too? Do you have to rebuild the entire PyPI catalog? Will your dependencies break?
This is the single most common question developers ask before compiling Python, and the answer is short enough to fit in a sentence — but the reasoning behind it is where the real understanding lives.
The Short Answer: Your Code Compiles, Theirs Doesn’t
When you compile Python with Py2Native, only the modules you explicitly pass to the build are turned into native machine code. Third-party libraries installed from PyPI are left exactly as they are — standard Python source, or the precompiled binary extensions they already shipped.
Everything in your project splits into two categories:
| Category | What it is | What Py2Native does |
|---|---|---|
| Your code | Modules you wrote — main.py, app/*.py, internal packages |
Compiled to native machine code (.pyd/.so/.dylib) |
| Dependencies | Anything resolved from PyPI into site-packages |
Left untouched, imported at runtime as normal |
The build command makes this boundary explicit. Whatever you list after the main module is what gets compiled — nothing else:
uv run py2native build main.py *.py
That’s it. Your site-packages directory is not in scope, and it never becomes in scope unless you deliberately point the build at it.
This is by design, not a limitation. If Py2Native tried to compile every dependency, it would break compatibility with the Python ecosystem, violate the licensing of half of PyPI, and fail on the packages that use C extensions written by other tools. Leaving dependencies alone is what makes the whole approach practical.
One note on mechanism: Py2Native uses Cython under the hood, but hides it completely. You write plain Python. You don’t write Cython syntax, you don’t create .pyx or .pxd files just to get things building, and you never run a manual cythonize step. “Write plain Python, get a native binary” is the entire workflow.
A Real-World Analogy: Shipping a Sealed Engine in a Standard Car
Picture a car manufacturer who wants to protect the design of the engine while still using commodity parts everywhere else.
- The engine is your proprietary code. It ships as a sealed, tamper-proof block. You can’t open it, you can’t read the blueprints, and only the manufacturer can service it. That’s your compiled module.
- The alternator, tires, brake pads, and battery are third-party libraries. They’re standard, off-the-shelf parts that any mechanic can buy and swap.
Why does the second part matter so much? Because the car still has to run. The alternator bolts onto the same mount, uses the same belt, produces the same voltage. Nothing about the sealed engine changes how the standard parts interface with the vehicle.
This isn’t just convenience — it’s a legal requirement in many cases. LGPL libraries must remain replaceable by the end user. If you weld a replaceable part into a sealed block, you’ve broken the license. Keeping third-party parts as standard, swappable components is exactly what compliance looks like in practice.
Py2Native works the same way. You ship a native binary, but the Python ecosystem underneath keeps operating exactly as it did before.
Under the Hood: How Py2Native Handles Dependencies
The build pipeline is worth walking through briefly, because the source globbing step is where the boundary is drawn.
uv run py2native build main.py *.py [--library] [--embed <dir>] [--wheel <dir>]
- Glob sources —
build.pyexpands the glob patterns you provided, under--baseif you set one. - Plugin dispatch —
pluginManager.extendBuild(args)runs, which is where platform plugins and (if licensed) the Pro plugin hook in. - Cython → C —
compiler.pygenerates abootstrap.pyxfromtemplates.py, invokes Cython with--embed --shared --embed-modules, and transpiles your.pyxfiles to C. - C → objects — the setuptools distutils compiler interface compiles the
.cfiles with platform-specific flags. - Link —
link_shared_object()for library mode,link_executable()for executable mode. - Wheel (optional) —
whl.pyproduces a PEP 427 wheel containing the compiled.pyd/.so. - Embed (optional) —
embed.pycreates auv-managed deployment directory.
Notice that at no point does the pipeline enumerate your installed dependencies. It compiles what you listed. Everything else resolves through the normal Python import machinery at runtime, which is left fully intact.
That “left intact” part is the key insight. Your compiled module is still a Python module as far as the interpreter is concerned — it just happens to be backed by machine code instead of a .py file. When it does import requests, the same sys.path walk, the same finders, the same site-packages lookup happens.
In library mode (--library), the wheel contains a single compiled shared library plus two generated files:
__init__.py— a meta-path finder that redirectsimport mypkg.submoduleinto the shared library__main__.py— the runnable entry point that enablespython -m mypkg
Both are required. The meta-path finder intercepts imports of your compiled modules and hands everything else — every third-party import — straight through to standard Python. If you want the deeper mechanics of the transpilation stage itself, see What Does Cython Actually Do to Your Python Code?.
Why This Matters: Compatibility, Licensing, and Practicality
Compatibility. Third-party libraries work as-is with minimal or no configuration. You don’t rebuild numpy from source, you don’t write C bindings to your ORM, and you don’t need a special fork of your HTTP client. That’s a direct consequence of leaving them in Python form.
Licensing. Because third-party libraries remain as Python source, LGPL libraries stay replaceable by the end user. Py2Native doesn’t statically link them or modify them, so the compliance story is the same as shipping them normally.
Practicality. Trying to compile every dependency is a fragile project that is often simply impossible. Some dependencies are pure C extensions with their own build systems. Some have platform-specific wheels. Some vendor other libraries internally. Compiling your own code and leaving the ecosystem alone is the only approach that scales to a real dependency graph.
Security. Your proprietary logic is protected — it exists only as machine code — while the open-source ecosystem continues to function normally around it.
Common Misconceptions (and What Actually Happens)
“Compiling Python means all dependencies get compiled too.” No. Only your code is compiled. Dependencies stay as Python source or as whatever precompiled wheels they arrived as.
“Third-party libraries will break or need patches.” They work unmodified, because the import system is preserved. From Python’s perspective, your compiled module is still a module.
“Monkey-patching is impossible after compilation.” Still possible. Third-party libraries remain Python source, so patching them at runtime behaves exactly as it did before you compiled. This is a documented constraint, not an oversight — it’s the price of leaving dependencies intact, and it’s a price worth paying.
“You need to write Cython or C to protect code.”
No. You write plain Python. No Cython syntax, no .pyx files by hand, no manual cythonize invocation, no C extensions to author and maintain. The hard way still exists — hand-writing Cython, managing .pxd declarations, wiring up a C toolchain yourself — but with Py2Native you don’t go down that road.
How Py2Native Makes This Zero-Config
The dependency handling requires no extra flags. It’s the default behavior:
uv run py2native build main.py app/*.py
Py2Native compiles your modules and leaves everything else alone. If you want packaging, you opt in:
# Build a PEP 427 wheel with the compiled modules
uv run py2native build main.py app/*.py --wheel dist
# Build a uv-managed, portable deployment directory
uv run py2native build main.py app/*.py --embed deploy
# Build a shared library instead of an executable
uv run py2native build main.py app/*.py --library
For details on those paths, see How to Ship a PEP 427 Wheel With Compiled Modules and How Py2Native’s --embed Builds a Portable Deployment Folder.
License verification with the Pro plugin
The core compiler is MIT-licensed. If you need to bind your binary to a signed license, the commercial Pro plugin (py2nativepro) adds JWT license verification, string compression, and the keygen, sign, and show commands:
uv run py2native keygen private.pem public.pem
uv run py2native sign --private private.pem payload.json license.dat
uv run py2native show --public public.pem license.dat
Then you pass the license into your build:
uv run py2native build main.py app/*.py --license license.dat --public public.pem
The plugin bakes the signature verification code and the public key into the executable — only the public key, never the private key. Elliptic-curve verification is handled by compiled code inside the product, with no third-party crypto libraries involved. This gives you maximum security and flexibility.
To wire it up, you include a .pxd file declaring the runtime verification hook:
# _p2n_license.pxd
from _p2n_bootstrap cimport _runtime_verify_es256_jwt
cdef _runtime_verify_es256_jwt(token, expected_iss=*, expected_aud=*)
And call it with the license string your users supply:
license = _runtime_verify_es256_jwt(
licenseString,
expected_iss="RSJ Software GmbH",
expected_aud="TimestampGIT",
)
That call site is the only thing your code needs. Everything else — signature verification, key material, the compiled verification path — is handled by the plugin.
FAQ
Do third-party libraries get compiled when I use Py2Native?
No. Py2Native compiles only the Python modules you specify — your custom code. Third-party libraries installed from PyPI remain as standard Python source or precompiled extensions and are not modified.
Will my dependencies still work after compilation?
Yes. Because third-party libraries are left untouched, they continue to work as-is with minimal or no configuration. The Python import system remains intact, so your compiled modules can import them normally.
What about LGPL compliance?
Since third-party libraries remain as Python source, LGPL libraries stay replaceable by the end user, which helps you meet compliance requirements. Py2Native does not statically link or modify them.
Can I still monkey-patch third-party libraries?
Yes. Monkey-patching is still possible because third-party libraries remain as Python source. Only your own modules are compiled to native code.
Are there any constraints I should know about?
A few, and they’re worth planning around: you can’t have modules with identical names across your source files, __init__.py files are skipped during compilation (they’re namespace markers, not compilable code), and in library mode __main__.py is excluded from sources because the wheel builder generates it. None of these affect how dependencies behave.
The Bottom Line
Compiling Python protects your code. It does not require — and should not attempt — compiling the rest of the ecosystem. Your modules become native machine code; your dependencies keep being dependencies. Compatibility holds, LGPL replaceability holds, and monkey-patching still works.
Py2Native makes that split the default. Write plain Python, run one build command, ship a native binary:
uv run py2native build main.py app/*.py
Install the open-source compiler, and add the Pro plugin when you need signed JWT license verification baked into the executable. Start at py2native.dev.
Related posts
- How to Ship a PEP 427 Wheel With Compiled Modules
- How Py2Native’s --embed Builds a Portable Deployment Folder
- Python Native Compiler Alternatives: What Each One Optimizes For