问题
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
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)
to the top of my script and
resource_path('main.kv')
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'
in case its of any use, line 147 is
GUI = Builder.load_file('main.kv')
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...
回答1:
Here is how I do it. In my main.spec
file, I specify my kv
file like this:
datas=[('gamescreen.kv', '.')]
Since it is in the same folder as my main.py
, The full path is not needed.
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)
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.
来源:https://stackoverflow.com/questions/61599799/main-kv-file-cant-be-found-in-pyinstaller-exe