How to protect and compile python source code into a .so library?

11,205

As mgilson mentioned in the comments, Cython is probably your best bet here. Generally speaking, you can use it to convert your pure-python source code into compiled extension modules. While the primary intent of Cython is for enhanced performance, there shouldn't be any barriers for using it for source-code protection. The extension modules it outputs aren't limited in any special ways so anything you were able to do from within Python before, you should be able to do from the Cython-generated extension modules. Cython does have a few known limitations in terms of supported features that may need to be worked around but, overall, it looks well suited to serving your purpose.

Share:
11,205
Mannaggia
Author by

Mannaggia

Updated on June 20, 2022

Comments

  • Mannaggia
    Mannaggia almost 2 years

    I would like to protect my python source code, I know there is no absolute protection possible, but still there should be some means to make it difficult enough to do or quite time-consuming. I want

    1) to remove all documentation, comments automatically and

    2) to systematically change the names of variables and functions within a module (obfuscation?), so that I can keep an external interface (with meaningful names) while the internal names of variables and functions are impossible to pronounce.

    Perhaps the best solution, which would make 1) and 2) redundant, is the following:

    3) Is there a simple way to compile python modules to .so libraries, with a clear interface and which can be used by other python modules? It would be similar as building C and C++ extensions with distutils, except that the source code is python itself rather than C/C++. The idea is to organize all "secret code" into modules, compile them, and then import them in the rest of the python code which is not considered secret.

    Again, I am aware that everything can be reverse-engineered, I think in pragmatic terms, most of the average developers would not be able to reverse-engineer code and even if they would be able, ethical/legal/timing reasons would make them think twice if they really want to work on this.