👉 项目官网:https://www.python-office.com/ 👈

👉 本开源项目的交流群 👈

github star gitee star atomgit star

AI编程 AI交流群

大家好,这里是程序员晚枫,正在all in AI编程实战,全网同名。

目标:把前面 10 讲的所有技巧,浓缩成 3 条可复制落地的流水线。
每项目 10 分钟,给你“源码 + spec + CI + 安装包”一键 fork 即用。


项目 1:CLI 小工具 AutoUpdate
需求:

  • 单文件 exe,带自动更新(GitHub Release API)
  • 体积 < 15 MB
  • 支持 Windows / macOS / Linux

11.1.1 目录

1
2
3
4
5
6
7
autocli/
├─ cli.py # click 写的命令行
├─ updater.py # 调用 GitHub API 下载新版本
├─ assets/
├─ cli.spec
├─ .github/workflows/release.yml
└─ requirements.txt

11.1.2 核心技巧

  • --exclude-moduletestssetuptoolspip 砍掉
  • 版本号自动生成:
1
2
3
# cli.spec
import os
VERSION = os.getenv("GITHUB_REF_NAME", "dev").lstrip("v")
  • 自更新逻辑
1
2
3
4
5
6
7
8
9
10
import requests, zipfile, pathlib, sys, os
def update():
r = requests.get("https://api.github.com/repos/xxx/autocli/releases/latest")
tag = r.json()["tag_name"]
if tag != VERSION:
url = next(a["browser_download_url"] for a in r.json()["assets"] if "windows" in a["name"])
tmp = pathlib.Path("autocli_new.exe")
tmp.write_bytes(requests.get(url).content)
os.replace(tmp, sys.executable)
os.execv(sys.executable, [sys.executable] + sys.argv[1:])

11.1.3 CI(精简版)

1
2
3
4
5
6
7
8
9
10
# .github/workflows/release.yml
strategy:
matrix: {os: [ubuntu-latest, windows-latest, macos-latest]}
steps:
- uses: actions/checkout@v4
- run: pip install pyinstaller -r requirements.txt
- run: pyinstaller cli.spec
- uses: softprops/action-gh-release@v2
with:
files: dist/*

11.1.4 交付物
Push tag v1.0.0 → Release 出现

  • autocli-linux
  • autocli.exe
  • autocli-macos

项目 2:PyQt 桌面应用 DataViz
需求:

  • 拖拽 CSV → 出交互图表
  • 支持高 DPI、暗黑模式
  • 提供 Windows 安装包 + macOS dmg

11.2.1 目录

1
2
3
4
5
6
7
8
9
dataviz/
├─ main.py
├─ ui/
│ ├─ main.ui
│ └─ resources.qrc
├─ icons/
├─ main.spec
├─ installer.iss
└─ build_macos.sh

11.2.2 spec 关键点

1
2
3
4
5
6
7
8
9
10
11
12
a = Analysis(
['main.py'],
datas=[('ui', 'ui'), ('icons', 'icons')],
hiddenimports=['PyQt5.QtPrintSupport'],
excludes=['PyQt5.QtWebEngine'], # 用不到
)
exe = EXE(
...
console=False,
icon='icons/app.ico',
upx_exclude=['Qt5Core.dll', 'Qt5Gui.dll'],
)

11.2.3 安装包

  • Windows:Inno Setup 脚本一键生成 DataViz_Setup.exe
  • macOS:create-dmg DataViz.app

11.2.4 暗黑/高 DPI

1
2
3
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

macOS 自动跟随系统深浅色,Windows 10 需 qt.conf 加 DPI 开关。


项目 3:ML 推理服务 MLServer
需求:

  • 打包为系统服务(Windows Service / systemd)
  • 仅 CPU 版 PyTorch,体积 < 200 MB
  • 支持插件式模型

11.3.1 目录

1
2
3
4
5
6
7
mlserver/
├─ service.py # FastAPI 服务
├─ models/ # 空目录,放 .pt
├─ plugins/ # 动态加载
├─ service.spec
├─ install_service.sh
└─ install_service.bat

11.3.2 spec 瘦身

1
2
3
4
5
6
a = Analysis(
['service.py'],
datas=[('models', 'models'), ('plugins', 'plugins')],
hiddenimports=['uvicorn.logging', 'torch.jit'],
excludes=['torch.cuda', 'torch.distributions', 'pandas.tests'],
)

11.3.3 系统服务脚本
Windows

1
sc create MLServer binPath= "%CD%\dist\service.exe"

Linux

1
2
3
sudo cp dist/service /usr/local/bin/mlserver
sudo cp mlserver.service /etc/systemd/system/
sudo systemctl enable --now mlserver

11.3.4 插件热更新
同第十讲思路:重启服务时 importlib.reload


11.4 一键 fork 仓库

GitHub 模板:github.com/yourname/pyinstaller-cookbook
包含以上 3 个项目完整源码、spec、CI、安装脚本。
Fork → 改名字 → Push tag → 5 分钟后即可拥有 3 平台 Release。


小结 & 结课

• CLI:单文件 + 自动更新 + 瘦身体积
• GUI:暗黑/高 DPI + 安装包 + 图标
• ML:系统服务 + 插件化 + 200 MB 以内

至此 12 讲闭环,你已掌握从“一行命令”到“商业级交付”的完整链路。
下一步:把模板套到自己的项目,开始第一次 Release!


大家在学习课程中有任何问题,欢迎+微信和我交流👉我的联系方式:微信、读者群、1对1、福利

扫一扫,领红包

美团红包

🎓 AI 编程实战课程

程序员晚枫专注AI编程培训,通过 《30讲 · AI编程训练营》,让小白也能用AI做出实际项目。帮你从零上手!