在Pyinstaller exe中找不到主KV文件 [英] Main KV File cant be found in Pyinstaller exe

查看:86
本文介绍了在Pyinstaller exe中找不到主KV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的智慧到此为止...因此,我有一个kivy应用程序,可以从解释器正常运行,并且在构建为目录时可以正常运行.但是,如何构建它似乎并不重要,生成的exe总是找不到我的main.kv文件.我的文件结构是,我基本上有图像和一堆屏幕.

I'm at my wits end here... So I have a kivy app that runs fine from the interpreter and fine when built as a directory. But it doesn't seem to matter how I build it, the resulting exe always fails to find my main.kv file. My file structure is that I basically have images and a bunch of screens.

main.py
main.kv
resources/image 1
         /image 2
         /kv_files/screen1
                  /screen2 

我已经解决了所有关于SO的类似问题,包括这一个 ,以及

I have been over all the similar question on SO including this one, and this one and this one. I think I have tried all of the options, most recently I have added

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.environ.get("_MEIPASS2",os.path.abspath("."))

return os.path.join(base_path, relative_path)

位于脚本顶部和

 resource_path('main.kv')

就在"app.run()"之前.在这一点上,我觉得我已经尝试了不同建议的所有其他组合.规格文件为

just before 'app.run()'. And at this point I feel like I've tried every other combination from the different suggestions. Spec file is

# -*- mode: python ; coding: utf-8 -*-
 from kivy_deps import sdl2, glew

block_cipher = None

a = Analysis(['C:\\Users\\nicks\\PycharmProjects\\Winapp\\main.py'],
         pathex=['C:\\Users\\nicks\\Desktop\\Winapp'],
         binaries=[],
         datas=[('C:\\Users\\nicks\\PycharmProjects\\Winapp\\main.kv', '.')],
         hiddenimports=['pkg_resources.py2_warn', '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)
a.datas += [('main.kv', 'C:/users/nicks/PyCharmProjects/Winapp/main.kv', 'DATA')]

exe = EXE(pyz, Tree('C:\\Users\\nicks\\PycharmProjects\\Winapp\\resources', 'DATA'),
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
      name='mainapp',
      debug=True,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      upx_exclude=[],
      runtime_tmpdir=None,
      console=True)

无论我如何尝试,命令行都会给我

It seems whatever I try, the command line gives me

Traceback (most recent call last):
File "main.py", line 147, in <module>
File "lib\site-packages\kivy\lang\builder.py", line 288, in load_file
FileNotFoundError: [Errno 2] No such file or directory: 'main.kv'

如果有任何用途,第147行为

in case its of any use, line 147 is

GUI = Builder.load_file('main.kv')

所以我可以看到为什么无法找到它是一个问题.我看过PyInstaller文档,了解a.datas和Tree应该如何结构化,所以我认为它们还可以,但是我仍然觉得有些根本性的东西让我无法理解.非常感谢所有帮助...

So I can see why not being able to find it is a problem. I've been over the PyInstaller docs about how a.datas and Tree are supposed to be structured so I think they are ok, but I still feel that there is somehting fundamental that I'm not getting. Any help at all is gratefully received...

推荐答案

这是我的操作方法.在我的main.spec文件中,我这样指定kv文件:

Here is how I do it. In my main.spec file, I specify my kv file like this:

         datas=[('gamescreen.kv', '.')]

由于它与我的main.py位于同一文件夹中,因此不需要完整路径.

Since it is in the same folder as my main.py, The full path is not needed.

要将sys._MEIPASS添加到资源路径,我使用(在main.py的顶部):

To add the sys._MEIPASS to the resource path, I use (at the top of main.py):

if getattr(sys, 'frozen', False):
    # this is a Pyinstaller bundle
    kivy.resources.resource_add_path(sys._MEIPASS)

此外,仅提醒您pyinstaller在命令行参数方面有一些奇怪的行为.请注意文档,其中讨论了有限的选项当以.spec文件作为参数运行pyinstaller时,它们实际上会起作用.在这种情况下,某些命令行选项将被忽略.

Also, just a reminder that the pyinstaller has some strange behavior concerning command line arguments. Note the documentation that discusses the limited options that actually have an effect when pyinstaller is run with a .spec file as an argument. Some command line options are silently ignored in that situation.

这篇关于在Pyinstaller exe中找不到主KV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆