【发布时间】:2020-06-22 15:39:39
【问题描述】:
我已经阅读了几十篇文章,包括 pyinstaller 文档,但无法弄清楚为什么我的 pyinstaller 可执行文件不断返回 pygame.error: Couldn't open data/items/switch.png。
switch.png 是脚本应该加载的许多图像中的第一个。
我正在从我的应用程序的根目录运行pyinstaller --add-data 'data:data' snake.py。这个director中唯一的东西是snake.py和data,其中data包含图像和音乐文件的子文件夹。
snake.spec 显示 datas=[('data', 'data')],,这对我来说是正确的。
我也尝试过使用 --onefile 修饰符,但每次仍然出现相同的错误。
更新:
我已确认文件在捆绑包中。我正在使用--onedir 来解决这个问题,'Dist' 目录中的目录结构反映了我的本地结构。使用--add-data="data:data",主 .py 文件旁边有一个“数据”文件夹,其中包含一个数据文件'database.xlsx' 和剩余数据文件的子目录。我收到以下消息,表明系统无法访问第一个数据文件:
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "cronga.py", line 559, in <module>
File "pandas/io/excel/_base.py", line 304, in read_excel
File "pandas/io/excel/_base.py", line 824, in __init__
File "pandas/io/excel/_xlrd.py", line 21, in __init__
File "pandas/io/excel/_base.py", line 353, in __init__
File "pandas/io/excel/_xlrd.py", line 36, in load_workbook
File "xlrd/__init__.py", line 111, in open_workbook
FileNotFoundError: [Errno 2] No such file or directory: 'data/database.xlsx'
[11168] Failed to execute script cronga
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
【问题讨论】:
-
也许您可以在可执行文件启动时记录
os.getcwd()的输出。我猜(假设你的 exe 正确打包)它是从不同的目录运行到解压的位置。 -
感谢您提供更多详细信息,您能否打印
os.path.abspath(path_to_your_data_file)以检查文件是否存在?您是否按照以下建议使用了sys._MEIPASS? minimal reproducible example 将更容易为您提供帮助。
标签: pygame conda pyinstaller