Hiding Python Code from non-programmers

痞子三分冷 提交于 2020-01-06 02:48:27

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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

python -m compileall ./


来源:https://stackoverflow.com/questions/24464913/hiding-python-code-from-non-programmers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!