Nuitka 优势
- 把 Python 编译成 C++,启动速度通常比 PyInstaller 快 30%~50%
- 运行时性能更好(Python 代码被编译)
- 反编译难度更高
环境准备
1. 安装 Nuitka
cd D:\work\visionx_project uv add --dev nuitka
2. 安装 C 编译器(二选一)
方案 A:Zig(推荐,轻量自动)
Nuitka 可以自动下载 Zig,在交互式终端执行编译命令时会提示下载,输入 Yes 即可。
如果需要手动下载:
# 下载 zig(根据最新版本调整 URL)
Invoke-WebRequest -Uri "https://ziglang.org/download/0.13.0/zig-windows-x86_64-0.13.0.zip" -OutFile "C:\zig.zip"
Expand-Archive -Path "C:\zig.zip" -DestinationPath "C:\zig"
# 添加到 PATH
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\zig\zig-windows-x86_64-0.13.0", "User")
方案 B:Visual Studio Build Tools
winget install Microsoft.VisualStudio.2022.BuildTools
安装时选择 “使用 C++ 的桌面开发” 工作负载。
3. 验证编译器
uv run python -m nuitka --version
最小验证
先创建一个简单测试,确认 Nuitka + pywebview 能正常编译:
# test_nuitka_pywebview.py
import webview
window = webview.create_window('Test', 'https://example.com', width=800, height=600)
webview.start()
编译:
uv run python -m nuitka --standalone --enable-plugin=pywebview --zig test_nuitka_pywebview.py
如果成功,会生成 test_nuitka_pywebview.dist 目录,运行:
test_nuitka_pywebview.dist\test_nuitka_pywebview.exe
完整项目编译
编译命令
uv run python -m nuitka \ --standalone \ --enable-plugin=pywebview \ --zig \ --assume-yes-for-downloads \ --windows-disable-console \ --windows-icon-from-ico=build/resources/icon.ico \ --include-package=server \ --include-package=server.services \ --include-package=server.services.handlers \ --include-package=server.core \ --include-package=server.core.utils \ --include-package=server.core.algorithms \ --include-package=server.project \ --include-data-dir=front/dist=front/dist \ --include-data-dir=config=config \ --include-data-files=uv.lock=uv.lock \ --output-dir=dist_nuitka \ main.py
参数说明
| 参数 | 说明 |
|---|---|
--standalone |
生成独立可执行文件,不依赖系统 Python |
--enable-plugin=pywebview |
自动处理 pywebview 相关依赖 |
--zig |
使用 Zig 作为 C 编译器(如果已安装 VS 可去掉) |
--assume-yes-for-downloads |
自动下载所需依赖 |
--windows-disable-console |
不显示控制台窗口 |
--windows-icon-from-ico |
设置 exe 图标 |
--include-package |
显式包含动态导入的 Python 包 |
--include-data-dir |
包含前端静态资源和配置文件 |
--output-dir |
指定输出目录 |
如果编译报错缺少模块
根据错误提示添加 --include-package 或 --include-module:
--include-package=missing_package --include-module=missing_module
动态导入的处理器注册
server/api.py 中通过 importlib 动态加载处理器:
_PROCESSOR_REGISTRY = {
'gamma': ('server.services.handlers.gamma_processor', 'GammaProcessorWrapper'),
...
}
Nuitka 静态分析可能无法发现这些导入,需要显式包含:
--include-module=server.services.handlers.gamma_processor --include-module=server.services.handlers.brightness_processor --include-module=server.services.handlers.gamut_processor --include-module=server.services.handlers.ir_drop_processor --include-module=server.services.handlers.power_processor --include-module=server.services.handlers.motion_blur_processor --include-module=server.services.handlers.frequency_processor --include-module=server.services.handlers.ppt_agent_processor --include-module=server.services.handlers.test_report_processor --include-module=server.services.handlers.excel_to_pdf_processor --include-module=server.services.handlers.auto_judge_processor --include-module=server.services.handlers.color_coordinate_processor --include-module=server.services.handlers.crosstalk_processor --include-module=server.services.handlers.dbv_processor --include-module=server.services.handlers.dms_processor --include-module=server.services.handlers.flicker_processor --include-module=server.services.handlers.gray_cct_duv_processor --include-module=server.services.handlers.uniformity_processor --include-module=server.services.handlers.svm_framework_processor --include-module=server.services.handlers.ca410_auto_test_processor
使用打包脚本
项目已提供 build/build_nuitka.py,在本地运行即可:
cd D:\work\visionx_project python build\build_nuitka.py
输出结果
编译成功后,输出目录结构:
dist_nuitka/
└── main.dist/
├── main.exe ← 入口程序
├── python3xx.dll ← Python 运行时
├── webview/ ← pywebview 相关文件
├── front/ ← 前端资源
├── config/ ← 配置文件
└── ... ← 其他依赖 DLL
启动速度对比
预期效果:
| 方案 | 大致启动时间 | 说明 |
|---|---|---|
| PyInstaller | 2~4 秒 | 需要自解压和导入 |
| Nuitka | 1~2 秒 | 编译后启动更快 |
实际效果取决于硬件、Python 导入优化程度和 WebView2 初始化时间。
常见问题
1. 缺少 C 编译器
FATAL: Error, cannot locate suitable C compiler.
解决:安装 zig 或 Visual Studio Build Tools。
2. 运行时缺少模块
ModuleNotFoundError: No module named 'xxx'
解决:添加 --include-package=xxx 或 --include-module=xxx。
3. 前端资源 404
确保 --include-data-dir=front/dist=front/dist 路径正确,且 main.py 中使用相对路径加载。
4. 数据库文件
db/panellab_data.db 不应该打包进去,应该让程序运行时自动创建。检查 database_extended.py 中数据库路径是否为相对路径。
优化建议
- 延迟导入:在
main.py和server/api.py中延迟所有重依赖导入 - 移除未使用的依赖:在
pyproject.toml中清理不用的包 - 使用 UPX:Nuitka 支持
--enable-plugin=upx压缩 DLL,但可能增加启动时间 - 测试 mshtml 后端:如果对兼容性要求不高,改用
gui_backend: mshtml进一步提升启动速度
下一步
- 在本地安装编译器
- 运行
build_nuitka.bat - 根据报错调整
--include-package/--include-module - 对比
dist_nuitka/main.dist/main.exe和dist/VisionX/VisionX.exe的启动速度













