对一个exe的找不到,找不到kv文件

问题描述 投票:0回答:1

[我在这里看到了一些相关的问题,并尝试了我发现的每个解决方案,例如thisthis,但我无法做到这一点。因此,问题很简单-从奇异果制成单个exe文件后,它抛出FileNotFoundError: [Errno 2] No such file or directory: 'main2.kv'。具有相关文件的EXE文件效果很好,但我不知道在构建单个EXE时我在做什么错。

我的规格文件:

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

from kivymd.tools.packaging.pyinstaller import hooks_path as kivymd_hooks_path
from kivy_deps import sdl2, glew
...
a = Analysis(['main.py'],
    ...
    # I tried to write here absolute path, relative path and this method
    datas=[('*.kv', '.')],
    hiddenimports=[],
    hookspath=[kivymd_hooks_path],
    ...
)

# I also tried to put import here - didn't help (when I did that I also tried to change the path in my .py file to 'Data\main2.kv')
# a.datas += [('main2.kv', 'D:\\prog\\Lotto\\main2.kv', 'DATA')]
...

# tried with Tree and without Tree
coll = COLLECT(exe, Tree('D:\\prog\\Lotto','Data'),
    ...
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    ...

在我尝试添加的.py文件中

if getattr(sys, 'frozen', False):
    kivy.resources.resource_add_path(sys._MEIPASS)

def resourcePath():
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS)
    return os.path.join(os.path.abspath("."))
...

if __name__ == "__main__":
    kivy.resources.resource_add_path(resourcePath())
    MainApp().run()

并同时尝试了上述两种方法-没什么。还尝试将PyInstaller 3.6更改为3.5版本。我不知道这是什么问题,这是我第一次尝试从Kivy中制作exe文件。

python kivy pyinstaller
1个回答
0
投票

几个小时后终于明白了。我首先以file格式打开kv文件,然后将其传递给Builder以设置编码。在那种情况下,我只需要手动将kv文件的路径定义为sys._MEIPASS + 'main2.kv',然后它就可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.