尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

使用 Nuitka 打包 Python 指南

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