启动可执行文件时程序因错误而停止

Program stops with error when starting executable

我有一个 运行 kivy-app,当我从 vscode 启动它时它工作正常。但是当我尝试用

制作一个 exe 文件时
pyinstaller --onefile --icon=icons/levermannApp.ico --exclude-module matplotlib LevermannApp.py

然后启动程序我收到此错误消息

[CRITICAL] [Window      ] Unable to find any valuable Window provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
sdl2 - Exception: SDL2: Unable to load image
  File "kivy\core\__init__.py", line 70, in core_select_lib
  File "kivy\core\window\window_sdl2.py", line 152, in __init__
  File "kivy\core\window\__init__.py", line 982, in __init__
  File "kivy\core\window\window_sdl2.py", line 311, in create_window
  File "kivy\core\window\__init__.py", line 1268, in create_window
  File "kivy\graphics\instructions.pyx", line 783, in kivy.graphics.instructions.RenderContext.__init__
  File "kivy\core\image\__init__.py", line 561, in __init__
  File "kivy\core\image\__init__.py", line 754, in _set_filename
  File "kivy\core\image\__init__.py", line 460, in load
  File "kivy\core\image\__init__.py", line 223, in __init__
  File "kivy\core\image\img_sdl2.py", line 47, in load

 Traceback (most recent call last):
   File "LevermannApp.py", line 28, in <module>
     class MyLayout(Widget):
   File "LevermannApp.py", line 29, in MyLayout
     Window.size = (550, 700)
 AttributeError: 'NoneType' object has no attribute 'size'
[49080] Failed to execute script LevermannApp

我的部分代码如下所示:

class MyLayout(Widget):
    Window.size = (550, 700)

class LevermannScore(App):
    def build(self):
        return MyLayout()

if __name__ == "__main__":
    LevermannScore().run()

我的规范文件如下所示:

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


block_cipher = None


a = Analysis(['LevermannApp.py'],
             pathex=['C:\Users\Polzi\Documents\DEV\Python-Private'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=['matplotlib'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='LevermannApp',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True , icon='icons\levermannApp.ico')

知道为什么程序 运行 可以 vscode 但不能执行吗? (过去我已经能够用 pyinstaller 为这个程序制作一个 exe 文件 - 但现在我再次尝试并得到上面的错误...)

我想我终于能够通过如下定义规范文件来解决它:

# -*- mode: python -*- 
 
import os
from kivy_deps import sdl2, glew
 
spec_root = os.path.abspath(SPECPATH)
block_cipher = None
app_name = 'LevermannApp'
win_icon = 'icons/levermannApp.ico'
 
a = Analysis(['LevermannApp.py'],
             pathex=['C:\Users\Polzi\Documents\DEV\Python-Private'],
             binaries=None,
             datas=[('*.kv', '.')],
             hiddenimports=['win32timezone'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name=app_name,
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          console=False,
          icon=win_icon)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=False,
               name=app_name)

然后用命令开始简单的编译

pyinstaller LevermannApp.spec

顺便说一句,它没有编译成单个 exe 文件(我在使用 pyinstaller 时通常会这样)——但这对我来说不是什么大问题,而且 exe 文件工作正常。 (我将把它与 Inno Setup 一起使用,最终编译成 setup.exe)