py文件加密打包成exe文件

python的py、pyc、pyo、pyd文件区别

  1. py是源文件;
  2. pyc是源文件编译后的文件;
  3. pyo是源文件优化编译后的文件;
  4. pyd是其他语言写的python库;

为什么选用Cpython

.pyd 文件是由 .c 文件生成的,.c 由源 .py 或 .pyx 文件生成,也就是说,无法反编译成 .py 或 .pyx 源文件,只能反编译成 .c 文件,这样就提高了一定代码安全性。

安装依赖项:

  • Cython(pip install Cython)
  • pyinstaller
  • python3

示例(以下文件在同一层目录)

目录结构

├───conf_file
│ ├───t1.conf
├───log_file
├───src
│ ├───main.py
│ ├───setup.sh
│ ├───t1.py
│ ├───t2.py
├───tool
│ ├───t1.exe

文件内容

1. file: t1.py

def printT1():
    print("Hello t1")

2. file: t2.py

def printT2():
    print("Hello t2")

3. file: main.py

import t1
import t2

if __name__ == "__main__":

    t1.printT1()
    t2.printT2()

4. file: setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules1 = [Extension("t1", ["t1.py"])]
setup(
    name = 't1',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules1
)

ext_modules1 = [Extension("t2", ["t2.py"])]
setup(
    name = 't2',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules1
)

5. file: pack.sh

# 如果使用的python3 ,以下指令请全部使用python3
# 仅适用win10
# 生成pyd文件
python setup.py build_ext --inplace
# pyinstall打包
pyinstaller.exe -D main.py
# 拷贝文件夹到 dist
cp -rf ../conf_file dist/main
cp -rf ../log_file dist/main
cp -rf ../tool dist/main
# 拷贝pyd文件到dist
cp -rf *.pyd dist/main
posted @ 2020-03-05 17:35  空水  阅读(2040)  评论(4编辑  收藏  举报