简体   繁体   中英

Pyinstaller is unable to run a Kivy Application due to KeyError

I'm packaging a Kivy Application using pyinstaller. The original application works fine, but when running from the dist folder, it fails due to a key error. Specifically, it starts the Kivy application, but then immediately closes due to the key error.

Error Message:

 Traceback (most recent call last):
   File "kivy\properties.pyx", line 861, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'screen_login'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "cli.py", line 5, in <module>
   File "kivy\app.py", line 949, in run
   File "kivy\app.py", line 919, in _run_prepare
   File "src\__main__.py", line 26, in build
   File "src\__main__.py", line 20, in __init__
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

The main.py with the resource_path function added.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import os
import sys
from kivy.resources import resource_add_path, resource_find


# Import files like
from CheckIn_Application.src.screens.login.login import LoginScreen
from CheckIn_Application.src.screens.checkin.checkin import CheckInScreen


def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


# Make all stuff modular
class WindowManager(BoxLayout):
    # Class Widgets
    login_widget = LoginScreen()
    check_in_widget = CheckInScreen()

    print(resource_path('maincheckin.kv'))

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # super(WindowManager, self).__init__(**kwargs)

        # Add Widget
        self.ids.screen_login.add_widget(self.login_widget)
        self.ids.screen_checkin.add_widget(self.check_in_widget)


class MainCheckin(App):
    def build(self):
        return WindowManager()


if __name__ == "__main__":
    # Main().run()
    if hasattr(sys, '_MEIPASS'):
        resource_add_path(os.path.join(sys._MEIPASS))
    MainCheckin().run()

And the associated.kv file

<WindowManager>:
    id: main_window

    ScreenManager:
        id: scrn_mngr_main

        Screen:
            id: screen_login
            name:'screen_login'
        Screen:
            id: screen_checkin
            name:'screen_checkin'

I think it has something to do with how pyinstaller interacts with the.kv file. I can replicate this error when I change the Screen's id to a string (ie. id: "screen_login versus id: screen_login) in the original code. I think Kivy makes it a point to not have their element id's identified as strings.

EDIT: Spec File:

# -*- mode: python ; coding: utf-8 -*-
import os
import sys

from pathlib import Path
from pylibdmtx import pylibdmtx
from pyzbar import pyzbar

from kivy_deps import sdl2, glew


block_cipher = None


a = Analysis(
             ['cli.py'],
             pathex=['C:/Users/username/PycharmProjects/DBlytics/CheckIn_Application'],
             binaries=[],
             datas=[('C:/Users/username/PycharmProjects/DBlytics/CheckIn_Application/src/maincheckin.kv','.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

# dylibs are not detected because loaded by ctypes.
# binaries accept the TOC format.  Use list comprehension.
a.binaries += TOC([
    (Path(dep._name).name, dep._name, 'BINARY')
    for dep in pylibdmtx.EXTERNAL_DEPENDENCIES + pyzbar.EXTERNAL_DEPENDENCIES
])

# A dependency of libzbar.dylib that PyInstaller does not detect
MISSING_DYLIBS = (
    Path('/usr/local/lib/libjpeg.8.dylib'),
)
a.binaries += TOC([
    (lib.name, str(lib.resolve()), 'BINARY') for lib in MISSING_DYLIBS
])


pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

a.datas += [('maincheckin.kv', 'C:/Users/username/PycharmProjects/DBlytics/CheckIn_Application/src/maincheckin.kv', 'DATA')]

exe = EXE(
          pyz,
          a.scripts,
          # *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
          [],
          exclude_binaries=True,
          name='cli',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )

coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='cli')

When adding the.kv file to the spec in this method, I notice that pyinstaller adds the.kv file to dist\cli\ folder and not to to the _MEIPASS temp folder.

The issue is that I have the code correct in the original, but somehow after I package it, this error gets thrown.

Can u share your.spec file with me? I had the same problem a couple of weeks ago. I think that you didn't specify your data source.

My.spec file is located in: ../denul2/ My code files (.py/.kv) are located in: ../denul2/project/

Look at this: https://imgur.com/a/pWCKPQ1

edit: Try to also add this: https://imgur.com/a/rGFOMw5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM