Packaging Python into .so files
Python is an excellent language for rapid development, with a rich ecosystem for solving data-digitization problems and complex text processing.
However, when the company deploys products directly on customer servers (the On-Premise model), we face a major intellectual-property protection problem:
Python is an interpreted language. The entire source code is essentially plain text. If we hand over the code directory as-is, the customer — or anyone with server access — can see the full core algorithms and the enterprise's technical know-how.
Even after deleting the source files and leaving only bytecode (.pyc), people can still decompile back to nearly original source in a few seconds using common tools.
To solve this problem, the engineering team applied this solution: Turn Python source code into "black-box" binary .so (Shared Object) files.
1. Core Idea: Bring Python Down to Machine Code
Languages such as C, C++, or Go compile all source code into machine code when shipping a product — only binary sequences that humans cannot read or understand.
Our idea is to borrow C's compilation mechanism to transform Python code:
[Python source code] ──(C-Extension compilation)──► [Binary .so file (machine code)]
- All business logic is converted and compiled into binary
.sofiles (on Linux). - When the system runs, the Python Runtime still loads and calls functions inside the
.sofile as usual, but the original source code has been removed completely. - Even if an attacker or competitor extracts the
.sofile from the server, they only receive binary machine code; reversing it back to the original source is infeasible.
2. Comparing Source-Code Protection Levels
[1. Pure Python source] ──► Anyone can open and read it immediately.
[2. Bytecode .pyc] ──► Easily decompiled with automated tools.
[3. Binary .so file] ──► Native machine code, absolute IP protection.
| Criterion | Pure Python source | Bytecode (.pyc) | Binary file (.so) |
|---|---|---|---|
| Readability | Public and clear | Requires tools to read | Unreadable |
| Decompilation | Not needed | Extremely easy | Nearly impossible |
| Execution speed | Normal | Faster at startup | More optimized (thanks to running as C code) |
| On-Premise safety | Very dangerous | High residual risk | Enterprise-grade |
3. Packaging a Binary Distribution
How do we hand over a system to the customer that both runs smoothly and completely removes original source files from the release?
We built an automated packaging pipeline using a Binary Distribution model:
- Automatic compilation: In the release-build pipeline, all Python source is automatically compiled into binary
.sofiles, after which the original.pyfiles are thoroughly removed. - Safe distribution: The distribution (Docker Image or deployment package) handed to the customer contains only executable binary files. The system starts and runs normally through the Python Runtime, but users or server administrators cannot view or interfere with the business logic inside.
As a result, all original source code is protected absolutely inside the company's internal infrastructure; not a single line of code is exposed in the actual deployment environment.
4. Practical Value Achieved
- Complete intellectual-property protection: The enterprise can confidently deploy the solution on any partner On-Premise infrastructure without worrying about decompilation or copying of core algorithms.
- Performance improvement: Executing directly as C machine code helps heavy compute and data-processing tasks outperform pure interpretation.
- Standardized release process: Distributing the product as a synchronized packaged binary makes On-Premise deployment consistent and meets the strict information-security standards of enterprise customers.
Conclusion
Software security is not only about blocking network attacks; it is also protecting the intellectual value and technology assets of the development team itself.
Packaging Python into .so is an effective balancing solution: it still leverages Python's flexible development speed, while ensuring the safety and reliability of binary machine code when releasing a product into a real production environment.
Shared by the engineering team at BK Hightech.

Written by Phan Van Tai
Software Engineer, BK Hightech
