Protecting python source code

10,073

Solution 1

Encrypt the python code and decrypt them when importing.

Step 1 - Implement your own module loader

You can use the importlib to implement your own python module loader. That is to say, you can change the import process to decrypt your encrypted python code.

But you may say the module loader is also written in python, the crypto key can be easily seen in the python script.

So let's go to next step.

Step 2 - Rewrite your module loader in C/C++

Thanks to cython, python module can be written in C/C++. The decryption code will be compiled to binary code. That's very hard to reverse engineer compared to py or pyc files.

Step 3 - Write a script to encrypt your source tree

This is the simplest part. :)

If you are interested in the topic, this project will help you - pyprotect.

Solution 2

There are two questions here: code protection and property. So licensing would be the second aspect, but it seems, from your question, licensing isn't your problem per-se. Some modules exist to protect your source code, like sourcedefender. It works pretty much like described by @lambda11 , though I have no idea if they use C/C++ to do their magic. The fact that they "do some magic" makes me nervous personally, code protection is one thing, but if it's a true concern, I guess the question is how hard it is to get to your code in the end. That's another story altogether. But if you look for short answers, well, people have done it previously.

Share:
10,073

Related videos on Youtube

Dimitrije Mirkovic
Author by

Dimitrije Mirkovic

Updated on June 04, 2022

Comments

  • Dimitrije Mirkovic
    Dimitrije Mirkovic almost 2 years

    I want to protect python script, since people most likely known to be jerks and steal someones work i would really like to protect it. Its sad how easily it can be stolen... copy paste my work that i spent months in and then someone changes bits and claims it for himself.

    How can i prevent anyone from seeing code? (i tried py2exe and similar, but that most often is suspicious to user since its .exe and i don't want anyone to be afraid of my software)

    Linux Python2 & Python3

  • Mario Palumbo
    Mario Palumbo over 3 years
    What it means: you must import all pip installed packages in main. Can I have an example?
  • Sobhy Elgraidy
    Sobhy Elgraidy over 3 years
    it means if you use third party package inside any .py file in your project then you must import this package in main .py file that you use it to run your project