Pyinstaller- 在可执行文件中包含资产 [英] Pyinstaller- Including assets in executable

查看:56
本文介绍了Pyinstaller- 在可执行文件中包含资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚如何将自定义资源打包到我的可执行文件中,我找到了一些信息,https://groups.google.com/forum/#!msg/pyinstaller/HcTTyFlPJHA/kqd4wnm7JhkJ

Cant figure out how to pack custom resources into my executable, i found this bit of info,https://groups.google.com/forum/#!msg/pyinstaller/HcTTyFlPJHA/kqd4wnm7JhkJ

我的资产文件夹包含 tkinter GUI 的图像,assets\images.png".某些 id 喜欢包含在可执行文件本身中.

My assets folder holds images for a tkinter GUI,"assets\images.png". Something id like to include in the executable itself.

所以我在我的 .spec 文件中的a.datas"下包含了这一行.

So i included this line under "a.datas,"in my .spec file.

Tree('assets', prefix='assets\'),

Tree('assets', prefix='assets\'),

然后用PyInstaller重新打包pyinstaller.py MipMapper-V1.2\MipMapper-V1.2.spec".这会生成一个不包含资产的 exe.我可以将资产文件夹复制到 exe 的目录并且它可以工作.是我的代码吗?它指的是资产"是事物的相对路径.所以我得到一个 -1 返回错误.它找不到资产.提前感谢您的帮助.

Then repacked with PyInstaller "pyinstaller.py MipMapper-V1.2\MipMapper-V1.2.spec". This produces an exe that does not have the assets included. I can copy the assets folder to the exe's directory and it works. Is it my code? it refers to "assets\" a relative path for things. So i get a -1 return error. It cannot find the assets. Thanks for any help in advance.

推荐答案

添加数据文件 和下一部分 使用来自模块的数据文件 在 pyinstaller文档.

Adding data files and next section using data files from module in pyinstaller docs.

是的,您应该在代码中使用包相对路径而不是普通的相对路径.因为包不是相对于执行二进制文件提取的,而是在临时目录中的某个地方.查找 pkg_resources 包.例如这个函数:

Yes, you should to use package relative path instead of plain relative in your code. Because package will be extracted not relative to execution binary, but somewhere in temp dir. Look for pkg_resources package. For example this function:

dir = resource_filename('your_package', 'app/samples')

要阐明如何在规范中定义数据,请查看示例:

To clarify how to define datas in spec look for example:

a = Analysis(['../trunk/__main__.py']
         ...
         datas=[('../src/trunk/your_package/app/samples/data.bin', 'your_package/app/samples')],
         ...

这篇关于Pyinstaller- 在可执行文件中包含资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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