Python PyInstaller 打包、Pyarmor加密等
这里讲打包、发布、加密
一图看懂整体流程
Python 源码
│
加密/编译(Pyarmor、Cython、Nuitka)
│
打包(PyInstaller/Nuitka)
│
️ 加固(混淆、壳保护、完整性校验)
│
发布(本地安装包、网盘等分享)这里有比较完整的示例,把大部分的内容都用上。
Win打包 应用程序
这里介绍Pyarmor+PyInstaller 打包。
注意:Pyarmor打包后,是加密的pyd文件,里面的import已经加密了,所以PyInstaller打包不会引入依赖。
解决方案:把程序拆分成main.py 需要加密.py 。 然后把"需要加密.py "里面的所有import都copy到“main.py”。 这样PyInstaller 打包自动识别依赖。
步骤 1:创建虚拟环境(推荐)
首先,创建一个新的虚拟环境,这样你可以控制项目中只安装和使用你需要的库,避免其他不必要的库被打包进去。
python -m venv myenv #创建虚拟环境
myenv\Scripts\activate #激活虚拟环境步骤 2:安装你的项目依赖【pyinstaller 是必须的,因为要用来打包】
pip install pyinstaller #pyinstaller 是必须的
pip install XXX playwright pyotp qrcode #(你的项目需要的库) 步骤 3:Pyarmor加密
一会儿打包,会更换为 ./dist/需要加密.py 这个文件,所以包名需要改一改。当然也可以不修改, 把 main.py 、 加密后的 py 、 pyarmor_runtime_000000文件夹 都放在一起 ,就不需要修改。
pip install pyarmor
pyarmor.exe gen main.py
# main.py 添加`from dist.pychrome_oo import *`
# 把dist\需要加密.py ,from pyarmor_runtime_000000 改为 from dist.pyarmor_runtime_000000 (就是加 dist.)步骤 4:PyInstaller 打包:
这条命令,--exclude-module 就是去掉多余的库,减少体积; --onefile 单文件 ; --add-data 添加额外文件,这里用的是分号;
python -m PyInstaller --clean --exclude-module tkinter --exclude-module test --exclude-module unittest --exclude-module email --exclude-module distutils --exclude-module xml --onefile --add-data "find_and_launch_chrome.vbs;." .\pychrome.pylinux 打包 应用程序
步骤 1:创建虚拟环境(推荐)
python -m venv myenv_ubuntu #创建虚拟环境
source myenv_ubuntu/bin/activate #激活虚拟环境步骤 2:安装你的项目依赖【pyinstaller 是必须的,因为要用来打包】
pip install pyinstaller #pyinstaller 是必须的
pip install XXX playwright pyotp qrcode #(你的项目需要的库) 步骤 3:Pyarmor加密
一会儿打包,会更换为 ./dist/需要加密.py 这个文件,所以包名需要改一改。当然也可以不修改, 把 main.py 、 加密后的 py 、 pyarmor_runtime_000000文件夹 都放在一起 ,就不需要修改。
pip install pyarmor
pyarmor gen main.py
# main.py 添加`from dist.pychrome_oo import *`
# 把dist\需要加密.py ,from pyarmor_runtime_000000 改为 from dist.pyarmor_runtime_000000 (就是加 dist.)步骤 4:PyInstaller 打包:
这条命令,--exclude-module 就是去掉多余的库,减少体积; --onefile 单文件 ; --add-data 添加额外文件,这里用的是冒号;
python3 -m PyInstaller --clean --exclude-module tkinter --exclude-module test --exclude-module unittest --exclude-module email --exclude-module distutils --exclude-module xml --onefile --add-data "find_and_launch_chrome.sh:." ./pychrome.py以下内容未整理:
| 类型 | 工具 | 用途 | 输出格式 |
|---|---|---|---|
| 加密保护 | Pyarmor / Cython / Nuitka | 防止源码泄漏 | .pyd、.so、加密文件 |
| 应用程序 | PyInstaller / Nuitka / cx_Freeze | 给非技术用户用 | .exe、.app、.bin |
| 安装包 | setuptools / poetry | 上架 pip 或内部 pip 安装 | .whl、.tar.gz |
| 容器 | Docker | 部署到服务器 | 镜像 |
Pyarmor(推荐,轻量加密 .py → 加密字节码)
pip install pyarmor
pyarmor gen main.pyNuitka(强加密 + 原生编译)
- 原理:Python → C → 本地机器码
.exe - 优点:几乎不可反编译,性能更高
- 缺点:打包慢、体积大、配置多
加壳保护(外部封装 .exe)
加壳 = 保护 .exe,防止 静态分析、逆向调试、内存注入
推荐加壳软件:
| 工具 | 系统 | 类型 | 优点 |
|---|---|---|---|
| ✅ Enigma Protector | Windows | 商用壳 | 防调试、防内存分析、内置授权 |
| VMProtect | Windows | 虚拟机壳 | 防逆向极强,价格贵 |
| Themida | Windows | 商业壳 | 防注入和内存读写强 |
| UPX(轻量) | All | 开源壳 | 减小体积,但易被脱壳 |
加固策略(提高破解难度)
| 加固方式 | 工具/做法 | 用途 |
|---|---|---|
| 变量名混淆 | pyminifier / obfuscator | 隐藏业务逻辑 |
| 动态加载模块 | base64 加密 + 动态执行 | 增加反编译难度 |
| 加密配置文件 | 对配置、密钥用 AES 等加密 | 防止破解接口 |
| 运行校验 | MD5 校验自身 + 路径校验 | 防修改运行逻辑 |
| 杀调试器/沙箱 | py-debugger-detect | 阻止运行在调试环境 |
| 自定义授权系统 | MAC 地址 + 签名校验 | 防止盗版流传 |
加壳保护(外部封装 .exe)
加壳 = 保护 .exe,防止 静态分析、逆向调试、内存注入
推荐加壳软件:
| 工具 | 系统 | 类型 | 优点 |
|---|---|---|---|
| ✅ Enigma Protector | Windows | 商用壳 | 防调试、防内存分析、内置授权 |
| VMProtect | Windows | 虚拟机壳 | 防逆向极强,价格贵 |
| Themida | Windows | 商业壳 | 防注入和内存读写强 |
| UPX(轻量) | All | 开源壳 | 减小体积,但易被脱壳 |
应用程序(目前只有win、等我打包过Linux再补充)
pip 分发的整体流程
源码开发(.py)
│
模块加密(Cython/.pyd)
│
打包为 wheel (.whl)
│
生成 setup.py / pyproject.toml
│
☁️ 上传 PyPI / 私有仓库(如 nexus)
│
用户 pip install xxx 安装
PyInstaller 打包
pip install pyinstaller #安装 PyInstaller
pyinstaller your_script.py #打包命令打包为库供别人通过 pip 安装:setuptools 工具
打包为 Docker 镜像
步骤 4:
4.1 运行 PyInstaller 打包
pyinstaller --onefile --clean --hidden-import=playwright.sync_api --exclude-module=tkinter your_script.py
python -m PyInstaller --clean --onefile .\your_script.py #win下使用 4.2 使用 .spec 文件进行精细控制(可选)
pyinstaller --onefile your_script.py # 生成一个 .spec 文件
修改.spec 文件
pyinstaller --clean --onefile your_script.spec
python -m PyInstaller --clean --onefile .\your_script.spec #win下使用基础命令介绍

浙公网安备 33010602011771号