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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM