python加密打包(保护python源代码)
1.编译为.so
在主py文件a.py
创建python文件build_pyd.py
如下:
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = "a_module",
ext_modules = cythonize(["a_helper.py", "config.py"])
)
其中a_helper.py
为掏空主文件a.py
的辅助文件,理由下面会提到
执行
python3 build_pyd.py build_ext --inplace
将生成a_helper.cpython-38-x86_64-linux-gnu.so
文件,(注意!!!由于pyinstaller加密打包只会加密.so依赖,因此上述掏空操作可达到保护a.py
源代码的目的,另外还需注意在a.py
中import a_helper.py
所用包)
2.加密打包
将a_helper.cpython-38-x86_64-linux-gnu.so
移至a.py
同级目录,执行
pyinstaller --key '密钥' a.py
再在生成的a.spec
中加入
pathex=['a.py绝对路径']
执行
pyinstaller a.spec
即可。