py文件加密打包成exe文件
Published on 2020-03-05 17:35 in 分类: pyhon3 with 空水
分类: pyhon3

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 @   空水  阅读(2061)  评论(4编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示