Cython - Hiding Python Code

 

Protect your Python scripts by compiling them into binary modules using Cython. This method hides your source code while keeping full functionality.

 

Installation 

pip install cython

 

Python Code to Hide

def sum(a, b):
    return a + b 

def sub(a, b):
    return a - b 

 

Generate the Compiled Code

from setuptools import setup, Extension
from Cython.Build import cythonize

exts = [
    Extension("core", ["core.py"]),  
]

setup(
    ext_modules=cythonize(exts),
)

 

python setup.py build_ext --inplace

May require updated Visual Studio Build Tools https://visualstudio.microsoft.com/visual-cpp-build-tools/

  • Desktop development with C++
  • MSVC v14.x C++ build tools
  • Windows 10 SDK (or 11 SDK)
     

The generated Compiled Code will look similar to this

MZ       ÿÿ  ¸       @                                      º

$       g[R	R	R	[fšP	Õ—	P	&Ÿ	P	Õ—
	Q	Õ—
	Z	Õ—	^	øw	Q	R*	×	S		S	ÃS	×	S	RichR	        d†
 !Ai        ð " , @   @      ´B        €                        À          `    
                  g  X   èg  d       ø      8           °     `   


...

 

Using the Compiled Code

from core import *

x = sum(15,3)
print(x)

 


Tested with:
Cython==3.1.4
Windows