Python Code Obfuscation

How do I protect Python code from being read by users?

Python, being a byte-code-compiled interpreted language, is very difficult to lock down. Even if you use a exe-packager like py2exe, the layout of the executable is well-known, and the Python byte-codes are well understood.

Usually in cases like this, you have to make a tradeoff. How important is it really to protect the code? Are there real secrets in there (such as a key for symmetric encryption of bank transfers), or are you just being paranoid? Choose the language that lets you develop the best product quickest, and be realistic about how valuable your novel ideas are.

If you decide you really need to enforce the license check securely, write it as a small C extension so that the license check code can be extra-hard (but not impossible!) to reverse engineer, and leave the bulk of your code in Python.

How to make my python scripts unreadable by users?

I would recommend using pyarmor https://wiki.python.org/moin/Pyarmor#:~:text=Pyarmor%20is%20a%20command%20line,code%20of%20each%20code%20object.

For more information: https://github.com/dashingsoft/pyarmor
https://pyarmor.readthedocs.io/en/latest/

how can i make the script unreadable?

You can't exactly make it unreadable

There is no way to make it so the user can't read your file, but we can get close.

1. Executables

You can use pyinstaller as mentioned in this post.

It is very easy to compile, and you can use the command shown below:

Make sure you install it!

pyinstaller [options] script [script …] | specfile
// or
pyinstaller

2. Byte Code

As @Gab mentioned, you can compile to byte code.

This a great solution as you don't need any external downloads, but the problem is that you must run it with the matching version of python. (Pointed out by @tdelaney).

If you want to use this solution, follow @Gab's post. (It's pretty great)

3. Shared Unix Environment

As @Samwise said, you can use a shared unix environment.

How about putting it in a shared Unix environment where other users have +x but not +r permission?

+x - Execute

+r - Read

4. Just don't do it.

There is really no reason for you to do this.

The only reason I can think of is if you are making this paid, and don't want people to make illegal copies. In that case, you can use this post (It's about gamedev, though some of the ideas are consistant)


Good luck with your script!



Related Topics



Leave a reply



Submit