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 文件,这样就提高了一定代码安全性。

安装依赖项:

  1. Cython(pip install Cython)
  2. pyinstaller
  3. python3

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

目录结构

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

文件内容

1.创建't1.py','t2.py','main.py'文件

 1 # file: t1.py
 2 def printT1():
 3     print("Hello t1")
 4     
 5     
 6 # file: t2.py
 7 def printT2():
 8     print("Hello t2")
 9     
10     
11 # file: main.py
12 import t1
13 import t2
14 
15 if __name__ == "__main__":
16 
17     t1.printT1()
18     t2.printT2()

2.创建'setup.py'文件

 1 # file: setup.py
 2 
 3 from distutils.core import setup
 4 from distutils.extension import Extension
 5 from Cython.Distutils import build_ext
 6 
 7 ext_modules1 = [Extension("t1", ["t1.py"])]
 8 setup(
 9     name = 't1',
10     cmdclass = {'build_ext': build_ext},
11     ext_modules = ext_modules1
12 )
13 
14 ext_modules1 = [Extension("t2", ["t2.py"])]
15 setup(
16     name = 't2',
17     cmdclass = {'build_ext': build_ext},
18     ext_modules = ext_modules1
19 )

3.创建‘pack.sh’文件

 1 # 如果使用的python3 ,以下指令请全部使用python3
 2 # 仅适用win10
 3  # 生成pyd文件
 4 python setup.py build_ext --inplace
 5  # pyinstall打包
 6 pyinstaller.exe -D main.py
 7  # 拷贝文件夹到 dist
 8 cp -rf ../conf_file dist/main
 9 cp -rf ../log_file dist/main
10 cp -rf ../tool dist/main
11  # 拷贝pyd文件到dist
12 cp -rf *.pyd dist/main

猜你喜欢

转载自www.cnblogs.com/jiangyibo/p/12421467.html