You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller打包的Kivy应用启动崩溃报“意外错误”

解决Kivy应用PyInstaller打包后启动崩溃问题

问题分析

你的Kivy应用在开发环境运行正常,但PyInstaller打包后启动即崩溃,大概率是路径解析错误、缺失隐式依赖、UPX压缩损坏库这几个原因导致的,结合你的SPEC文件,给出以下修复方案:

修复步骤

  • 开启调试模式查看错误详情
    把SPEC文件中EXE部分的debug=False改为debug=True,保持console=True,这样启动打包后的程序时,控制台不会自动关闭,能直接看到崩溃的具体错误信息,方便定位问题。

  • 修复Windows路径解析问题
    Windows系统中路径需用双反斜杠或者原始字符串,避免转义字符解析错误:

    • 将Analysis中的脚本路径改成[r'C:\kivy_venv\Prototype\Prototype.py']
    • 将COLLECT中的Tree('C:\kivy_venv\Prototype\')改成Tree(r'C:\kivy_venv\Prototype')
  • 添加隐式依赖声明
    PyInstaller无法自动检测到Kivy部分模块、numpy/scipy/pandas的子模块,需在hiddenimports中显式添加:

    hiddenimports=['kivy.storage.jsonstore', 'numpy', 'scipy', 'pandas', 'scipy._lib.messagestream'],
    
  • 关闭UPX压缩
    UPX压缩可能损坏Kivy依赖的二进制库,把EXE和COLLECT中的upx=True改为upx=False。

  • 确保资源文件被正确包含
    如果应用依赖其他资源文件(如配置模板、图标等),需在datas中声明,比如:

    datas=[('your_resource_folder', 'your_resource_folder')],
    

    你提到的json文件已在_internal目录,若为应用运行时自动生成则无需额外处理;若为预加载的配置文件,则需手动添加到datas。

修改后的完整SPEC文件

# -*- mode: python ; coding: utf-8 -*-

from kivy_deps import sdl2, glew

a = Analysis(
    [r'C:\kivy_venv\Prototype\Prototype.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=['kivy.storage.jsonstore', 'numpy', 'scipy', 'pandas', 'scipy._lib.messagestream'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='Prototype',
    debug=True,
    bootloader_ignore_signals=False,
    strip=False,
    upx=False,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe, Tree(r'C:\kivy_venv\Prototype'),
    a.binaries,
    a.zipfiles,
    a.datas,
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    strip=False,
    upx=False,
    upx_exclude=[],
    name='Prototype',
)

测试方法

  1. 保存修改后的SPEC文件
  2. 执行打包命令:pyinstaller Prototype.spec
  3. 进入dist/Prototype目录,运行Prototype.exe,查看控制台输出的错误信息(如果仍有问题)

内容的提问来源于stack exchange,提问作者Abe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:47:10