Information Security 10 min read

Techniques for Protecting Python Source Code: .pyc Distribution, Obfuscation, py2exe, and Cython

This article explains various methods to protect Python source code—including distributing compiled .pyc files, applying code obfuscation, packaging with py2exe, and compiling with Cython—detailing their concepts, implementation steps, advantages, and limitations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Techniques for Protecting Python Source Code: .pyc Distribution, Obfuscation, py2exe, and Cython

Due to Python's dynamic nature and open‑source characteristics, protecting Python code is challenging, prompting the community to explore encryption, obfuscation, and packaging solutions.

1 Distribute .pyc Files

1.1 Idea

The Python interpreter compiles source to .pyc bytecode, which can be executed directly; the binary .pyc file is not human‑readable.

1.2 Method

Compile .py files to .pyc using the standard library compileall module:

<code>python -m compileall &lt;src&gt;</code>

Then remove the original .py files, e.g.:

<code>find &lt;src&gt; -name '*.py' -type f -print -exec rm {} \;</code>

1.3 Advantages

Simple, raises the barrier for casual source inspection.

Cross‑platform: .py runs anywhere, so the compiled .pyc does too.

1.4 Disadvantages

Bytecode is interpreter‑specific; compatibility issues may arise.

Decompilers (e.g., uncompyle6 ) can recover source relatively easily.

2 Code Obfuscation

2.1 Idea

Obfuscation makes code harder to understand by removing comments, altering indentation, renaming identifiers, inserting dead code, etc.

2.2 Methods

Method 1: Use the online tool oxyry to obfuscate Python code.

<code># coding: utf-8
class A(object):
    """ Description """
    def __init__(self, x, y, default=None):
        self.z = x + y
        self.default = default
    def name(self):
        return 'No Name'

def always():
    return True

num = 1
a = A(num, 999, 100)
a.name()
always()</code>

Obfuscated output (oxyry) mainly changes comments, parameter names and spacing.

Method 2: Use the pyobfuscate library.

<code># coding: utf-8
if 64 - 64: i11iIiiIiiif 65 - 65: O0 / iIii1I11I1II1 % OoooooooOO - i1IIcclass o0OO00 ( object ) :
  if 78 - 78: i11i . oOooOoO0Oo0O
  ... (truncated for brevity)</code>

2.3 Advantages

Simple, raises the difficulty of reverse engineering.

Good compatibility as long as logic remains unchanged.

2.4 Disadvantages

Works only on single files; multi‑module projects are not handled.

Structure remains unchanged; bytecode can still be extracted.

3 Using py2exe

3.1 Idea

py2exe packages Python scripts into Windows executables by bundling compiled .pyc files with required libraries.

3.2 Method

1) Create an entry script hello.py :

<code>print 'Hello World'</code>

2) Write setup.py :

<code>from distutils.core import setup
import py2exe
setup(console=['hello.py'])</code>

3) Build the executable:

<code>python setup.py py2exe</code>

The resulting file is dist\hello.exe .

3.3 Advantages

Produces a ready‑to‑run Windows executable, simplifying distribution.

Higher cracking barrier than plain .pyc .

3.4 Disadvantages

Windows‑only.

The executable still contains .pyc files that can be decompiled.

4 Using Cython

4.1 Idea

Cython compiles .py / .pyx to C source ( .c ) and then to shared libraries ( .so on Unix, .pyd on Windows), making reverse engineering harder while also improving performance.

4.2 Method

1) Write hello.pyx (or hello.py ) containing the Python code.

<code>def hello():
    print('hello')</code>

2) Create setup.py :

<code>from distutils.core import setup
from Cython.Build import cythonize
setup(name='Hello World app', ext_modules=cythonize('hello.pyx'))</code>

3) Build the extension:

<code>python setup.py build_ext --inplace</code>

4) Use the compiled module:

<code>python -c "from hello import hello; hello()"</code>

4.3 Advantages

Compiled binaries ( .so / .pyd ) are difficult to crack.

Provides performance gains.

4.4 Disadvantages

Compatibility varies across OS versions; recompilation may be needed.

Not all Python code is supported; fixing incompatibilities can be costly.

- END -

Compilationobfuscationcode protectionCythonpy2exe
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.