惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
博客园 - 聂微东
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
美团技术团队
S
Secure Thoughts
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
T
Tailwind CSS Blog
月光博客
月光博客
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
S
Security @ Cisco Blogs
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
S
Security Affairs
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
O
OpenAI News
L
Lohrmann on Cybersecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - LungGiyo

frp 教程 nginx 跨域解决 001 局域网其他电脑也能访问wsl服务 001 nvm 管理不同版本的 node 与 npm 关于IP送中的影响和解决办法 Antigravity google ai 接入高德MCP 1.3.2 git使用ssh密钥 连接远程仓库 dynadot域名 托管到cloudflare , 使用cloudflare 的CDN功能 003 二进制日志-binlog Django 项目开发整体步骤(0 开始)+进阶 磊科N60Pro刷机 uni的页面解析 前端技术知识扫盲篇 使用 Lucky 为群晖 / 极空间 NAS 全服务启用 HTTPS 安全访问 00 docker 命令总结 001 Python 基础 麒麟系统升级openssh至openssh-10.0p1 零信任访问控制系统aTrust 蓝屏 001 安装wsl 2 && 开启 WSL2 Oxidized的model
Python PyInstaller 打包、Pyarmor加密等
LungGiyo · 2025-05-09 · via 博客园 - LungGiyo

这里讲打包、发布、加密

一图看懂整体流程

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 #(你的项目需要的库) 

步骤 3Pyarmor加密

一会儿打包,会更换为 ./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.)

步骤 4PyInstaller 打包:

这条命令,--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.py

linux 打包 应用程序

步骤 1创建虚拟环境(推荐)

python -m venv myenv_ubuntu #创建虚拟环境
source myenv_ubuntu/bin/activate #激活虚拟环境

步骤 2安装你的项目依赖【pyinstaller 是必须的,因为要用来打包】

pip install pyinstaller #pyinstaller 是必须的
pip install XXX playwright pyotp qrcode #(你的项目需要的库) 

步骤 3Pyarmor加密

一会儿打包,会更换为 ./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.)

步骤 4PyInstaller 打包:

这条命令,--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.py

Nuitka(强加密 + 原生编译) 

  • 原理:Python → C → 本地机器码 .exe
  • 优点:几乎不可反编译,性能更高
  • 缺点:打包慢、体积大、配置多

加壳保护(外部封装 .exe)

加壳 = 保护 .exe,防止 静态分析逆向调试内存注入

推荐加壳软件:

工具系统类型优点
Enigma ProtectorWindows商用壳防调试、防内存分析、内置授权
VMProtectWindows虚拟机壳防逆向极强,价格贵
ThemidaWindows商业壳防注入和内存读写强
UPX(轻量)All开源壳减小体积,但易被脱壳

加固策略(提高破解难度)

加固方式工具/做法用途
变量名混淆pyminifier / obfuscator隐藏业务逻辑
动态加载模块base64 加密 + 动态执行增加反编译难度
加密配置文件对配置、密钥用 AES 等加密防止破解接口
运行校验MD5 校验自身 + 路径校验防修改运行逻辑
杀调试器/沙箱py-debugger-detect阻止运行在调试环境
自定义授权系统MAC 地址 + 签名校验防止盗版流传

加壳保护(外部封装 .exe)

加壳 = 保护 .exe,防止 静态分析逆向调试内存注入

推荐加壳软件:

工具系统类型优点
Enigma ProtectorWindows商用壳防调试、防内存分析、内置授权
VMProtectWindows虚拟机壳防逆向极强,价格贵
ThemidaWindows商业壳防注入和内存读写强
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下使用

基础命令介绍