How to distribute Python program?

This article is for Python beginners who wish to study how to distribute a Python program.

This article will introduce 4 methods to distribute a Python program, all of which can be used on both Python2 and Python3.

1. Distribute PY files

Assume you have a Python program which has below source file structure:

.
|-- main.py
|-- wsgi.py
|-- config.py
\-- app
    |-- __init__.py
    |-- app.py
    |-- models.py
    |-- views.py
    |-- apis.py
    \-- xxx.py

The simplest distribution method is to ship above PY files to people who has same Python environment installed.

Your source code is public.

1. Distribute PYC files

This method requires you to compile Python source files firstly, then cleanup the folder structure to make it look like as following:

For Python3.x, filename.cpython-3x.pyc needs to be renamed as filename.pyc and moved out of __pycache__ folder.

.
|-- main.pyc
|-- wsgi.pyc
|-- config.pyc
\-- app
    |-- __init__.pyc
    |-- app.pyc
    |-- models.pyc
    |-- views.pyc
    |-- apis.pyc
    \-- xxx.pyc

Now you can distribute this program by shipping above PYC files to people who has same Python environment installed.

This method is almost same as method1 except it does not expose source code directly.

But please note that PYC file can be decompiled very easily by a Python Decompiler.

3. Distribute EXE files

method1 and method2 require the target machine must installed the same Python environment, it's inconvenient for customer.

Following packager tools can freeze Python scripts into standalone executables (one exe or one folder) which will be able to run without requiring a Python installation.

This method can protect Python source code much better than method2, but keep in mind that it still can be decompiled easily.

The layout of these executables which created by above packager tools are well-known, PYC files can be extracted from the EXE and then decompiled by a Python Decompiler.

4. Translate to C files

Cython and Nuitka can translate Python source code to equivalent C code, and then you can use a C compiler to build it into a native binary.

The binary can be run without requiring a Python installation and its performance is much better than method3.

Python source code can be protected very well by using this method, basically it is impossible to be decompiled.

Anything can be reverse-engineered and modified if there's enough benefit.

Summary

This article introduced 4 methods to distribute a Python program:

  • Distribute PY files:
    • Target machine must has same Python installed.
    • Source code is exposed.
  • Distribute PYC files:
    • Target machine must has same Python installed.
    • Source code is hide but can be decompiled very easily.
  • Distribute EXE files:
    • Target machine does not require a Python installation.
    • Source code is proteced well but still can be decompiled easily.
    • Performance is the worst one.
  • Translate to C files:
    • Target machine does not require a Python installation.
    • Source code is proteced very well and almost impossible to be decompiled.
    • Performance is the best one.
Back to homepage or read more recommendations: