1

How can I obfuscate / hide my Python code from the customer, so that he cannot change the source how he likes to?

I know there is no effective way to hide Python code, so that there is no way to read it. I just want a simple protection, that someone who doesn't really know what he is doing cannot just open the source files with a text editor and make changes or understand everything easily with no effort. Because my code is written really understandable, I'd like to hide the main principles I used at the first place.

If someone really wants to understand what I have done, he will. I know that.

So is there a common method you use to make a simple protection for python code?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
uloco
  • 2,283
  • 4
  • 22
  • 37
  • 3
    Look at the "Use Compiled Bytecode" section of [this link](https://wiki.python.org/moin/Asking%20for%20Help/How%20do%20you%20protect%20Python%20source%20code%3F) – sshashank124 Jun 28 '14 at 08:06
  • Are the .pyc files portable between different environments/os, if I use the same python version? – uloco Jun 28 '14 at 08:08
  • The bytecode is *interpreted*, so it will be portable. – merlin2011 Jun 28 '14 at 08:13
  • I knew about this method but wasn't sure if it is the right approach. Thanks for clearance. – uloco Jun 28 '14 at 08:17

5 Answers5

7

Just compile it to bytecode using the method in this answer.

import py_compile
py_compile.compile("file.py")

The pyc file can be distributed in place of the py file.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

You can try converting them into executable files using something like pyinstaller or py2exe although that will increase the distribution size.

user2963623
  • 2,267
  • 1
  • 14
  • 25
1

You can put all your python files in a zip file, and put the zip file on the python path before or during startup of your application. If you name your zip file something other than .zip it'll prevent the technically clueless from finding the source code.

To launch your app, have your main module unzipped, and have it update the python path itself before importing any of the zipped source.

In your main module:

__import__('sys').path.append('./source.dat')
import mymodule
...

if __name__ == '__main__':
    ....

You'll want to include .pyo and .pyc files in the zip archive otherwise imports will be slow. See https://docs.python.org/2/library/zipimport.html for details.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
1

As suggested in the same post as the one in the accepted answer you will be better off using compileall :

python -m compileall ./
Community
  • 1
  • 1
MediaVince
  • 479
  • 1
  • 8
  • 13
0

You may do

  1. Code Obfuscation
  2. Generate byte code

There is two way to generate byte code

  1. Command line
  2. Using python program

If you are using command line use python -m compileall <argument> to compile python code to python binary code. Ex: python -m compileall -x ./*

Or, You can use this code to compile your library into byte-code.

import compileall
import os

lib_path = "your_lib_path"
build_path = "your-dest_path"

compileall.compile_dir(lib_path, force=True, legacy=True)

def compile(cu_path):
    for file in os.listdir(cu_path):
        if os.path.isdir(os.path.join(cu_path, file)):
            compile(os.path.join(cu_path, file))
        elif file.endswith(".pyc"):
            dest = os.path.join(build_path, cu_path ,file)
            os.makedirs(os.path.dirname(dest), exist_ok=True)
            os.rename(os.path.join(cu_path, file), dest)

compile(lib_path)
Prashant
  • 394
  • 1
  • 6
  • 18