Back to BlogSoftware Engineering

Packaging Python into .so Files

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 .so files (on Linux).
  • When the system runs, the Python Runtime still loads and calls functions inside the .so file as usual, but the original source code has been removed completely.
  • Even if an attacker or competitor extracts the .so file 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.
CriterionPure Python sourceBytecode (.pyc)Binary file (.so)
ReadabilityPublic and clearRequires tools to readUnreadable
DecompilationNot neededExtremely easyNearly impossible
Execution speedNormalFaster at startupMore optimized (thanks to running as C code)
On-Premise safetyVery dangerousHigh residual riskEnterprise-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 .so files, after which the original .py files 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

  1. 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.
  2. Performance improvement: Executing directly as C machine code helps heavy compute and data-processing tasks outperform pure interpretation.
  3. 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.

Phan Van Tai

Written by Phan Van Tai

Software Engineer, BK Hightech

Ready to build something great?

Tell us about your project and we'll get back to you within a day.

Get in Touch