为什么我的pyinstaller可执行文件无法访问数据文件? [英] Why can't my pyinstaller executable access data files?

查看:100
本文介绍了为什么我的pyinstaller可执行文件无法访问数据文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了数十篇文章,包括pyinstaller文档,并且无法弄清为什么我的pyinstaller可执行文件不断返回 pygame.error:无法打开data/items/switch.png .

I've been reading dozens of posts, including the pyinstaller docs, and cannot figure out why my pyinstaller executable keeps returning pygame.error: Couldn't open data/items/switch.png.

switch.png 是脚本应加载的许多图像中的第一个.

switch.png is the first of many images the script should be loading.

我正在从应用程序的根目录运行 pyinstaller --add-data'data:data'snake.py .该导演中唯一的内容是 snake.py data ,其中 data 包含图像和音乐文件的子文件夹.

I'm running pyinstaller --add-data 'data:data' snake.py from the root directory of my application. the only things in this director are snake.py and data where data contains subfolders for image and music files.

snake.spec 显示 datas = [('data','data')],对我来说正确.

我还尝试过使用-onefile 修饰符进行此操作,但每次仍然会遇到相同的错误.

I've also tried this with the --onefile modifier and still get the same error every time.

更新:

我已验证文件在捆绑包中.我正在使用-onedir 进行故障排除,并且'Dist'目录中的目录结构反映了我的本机结构.使用了-add-data ="data:data" ,并且在主.py文件旁边有一个"Data"文件夹,其中包含一个数据文件'database.xlsx'和其余数据文件的子目录.我收到以下消息,指示系统无法访问第一个数据文件:

I've verified the files are in the bundle. I'm using --onedir to troubleshoot this, and the directory structure within the 'Dist' directory mirrors that of my native structure. Used --add-data="data:data" and there is a single 'Data' folder alongside the main .py file which contains one data file 'database.xlsx' and subdirectories for the remaining data files. I'm receiving the following message which indicates the system cannot access the first data file:

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.

推荐答案

.spec文件是什么样的?这是有关添加数据文件的PyInstaller文档

What does your .spec file look like? Here's the PyInstaller docs on adding data files.

基本上,您需要添加以下内容:

Basically you need to add something like:

a = Analysis(...
 datas=[ ('the.wav', '.') ],
 ...
 )

这会将您的声音文件('the.wav')放入已编译应用程序的根目录(第二个参数.").从您的问题来看,您似乎想复制整个数据目录.问题出在那些文件的相对路径上.

This will put your sound file ('the.wav') into the root directory of your compiled application (the second argument, '.'). From your question, it looks like you want to copy your entire data directory. The issue will be with the relative path of those files.

然后在您的应用程序中,您可以检查您是从源代码运行还是作为已编译的可执行文件运行.我使用了一个辅助函数:

Then in your application you can check if you're running from source or as a compiled executable. I use a helper function:

def my_path(path_name):
    """Return the appropriate path for data files based on execution context"""
    if getattr( sys, 'frozen', False ):
        # running in a bundle
        return(os.path.join(sys._MEIPASS, path_name))
    else:
        # running live
        return path_name

因此,您的应用程序代码将类似于:

So then your application code will look something like:

the_sound = pygame.mixer.Sound(my_path("the.wav"))

在尝试调试可执行文件时,它可以帮助打开命令行并从那里运行可执行文件,如果您已经创建了控制台应用程序,则可能仍会看到回溯.

When trying to debug your executables, it can help to open a command line and run your executable from there, if you've created a console application, then you may still see a traceback that occurs.

这篇关于为什么我的pyinstaller可执行文件无法访问数据文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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