下面给你一份 PyInstaller 打包 EXE 与常见的 EXE 反编译方法(含注意事项) 的清晰总结,适合入门与排查问题:
🐍 PyInstaller:编译 EXE 与反编译指南
一、PyInstaller 打包 EXE(编译)
✅ 1. 安装 PyInstaller
pip install pyinstaller
✅ 2. 最简单打包
pyinstaller your_script.py
生成目录:
build/dist/your_script/your_script.spec
运行 EXE:dist/your_script/your_script.exe
🔒 3. 常用打包参数(强烈推荐)
✔ 单文件打包(–onefile)
pyinstaller -F your_script.py
✔ 去除命令行窗口(GUI程序)
pyinstaller -F -w your_script.py
✔ 添加图标
pyinstaller -F -i myicon.ico your_script.py
✔ 包含额外文件(如模型、配置)
pyinstaller -F your_script.py --add-data "config.yaml;."
注意:Windows 使用 ;,Linux / macOS 使用 :。
🔧 4. 打包后常见问题
❗ 运行时报错 “no module named xxx”
通常原因:依赖未被自动检测
解决:
pyinstaller -F your_script.py --hidden-import package_name
❗ 程序在其他电脑打不开(缺组件)
最常见:
- 运行环境缺微软 VC++ 运行库
- 路径中有中文导致路径解析错误
- 权限不足(UAC 拦截)
🧐 二、EXE 反编译(学习用途)
⚠️ 注意:反编译工具仅用于自己软件分析与学习,不得用于逆向破解他人软件!
PyInstaller 打包的 EXE 并不是完全不可逆,现有工具可以 80% 恢复代码。
🔓 1. pyinstxtractor(从 EXE 提取 pyc)
GitHub:pyinstxtractor.py
使用方式:
python pyinstxtractor.py your_script.exe
输出内容里会包含:
.pyc文件- 程序资源
- 配置文件等
🔓 2. 反编译 pyc → py
用 uncompyle6 或 decompyle3:
pip install uncompyle6
uncompyle6 extracted_file.pyc
🔐 3. 如何增加反编译难度?(不是绝对安全)
- 使用 UPX 压缩
pyinstaller -F your.py --upx-dir UPX_PATH
- 使用代码混淆器(如 pyarmor)
- 使用 Cython 将核心代码生成 C 扩展
- 关键算法放到服务端,不放在本地
⚠️ 单纯 PyInstaller 打包 ≠ 安全保护。
📦 三、总结
| 操作 | 工具/命令 |
|---|---|
| PyInstaller 打包 | pyinstaller -F your.py |
| 单文件 | -F |
| GUI | -w |
| 图标 | -i xxx.ico |
| 隐藏依赖 | --hidden-import |
| 反编译 EXE | pyinstxtractor |
| pyc → py | uncompyle6 / decompyle3 |
发表回复