gpt4 book ai didi

python - Pyinstaller 和 --onefile : How to include an image in the exe file

转载 作者:太空狗 更新时间:2023-10-29 21:26:14 25 4
gpt4 key购买 nike

我已经使用 Pyinstaller 创建了一个 exe 文件。

pyinstaller.exe --onefile --icon='Loco.ico program.py

在该程序中,我在绘图中包含了一张图像,当我在其文件夹中单独运行该程序时,我得到以下信息:

IOError: [Errno 2] No such file or directory: 'Logo.png'

一种解决方案是将图像包含在 exe 的文件夹中,如下面的链接所示:

pyinstaller does not show images and icon

不过话又说回来 --onefile 的全部要点就是拥有那个,而不需要另外的图像。我认为解决方案可能在此链接中,但我没有理解它。

Bundling data files with PyInstaller (--onefile)

我的规范文件如下所示:

# -*- mode: python -*-
a = Analysis(['AMOS_Visualizer.py'],
pathex=['C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)

pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='AMOS_Visualizer.exe',
debug=False,
strip=None,
upx=True,
console=True , icon='AMOS.ico')

最佳答案

编辑:

我相信我找到了解决问题的方法。

# -*- mode: python -*-
a = Analysis(['AMOS_Visualizer.py'],
pathex=['C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)

for d in a.datas:
if 'pyconfig' in d[0]:
a.datas.remove(d)
break

a.datas += [('Logo.png','C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\Logo.png', 'Data')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='AMOS_Visualizer.exe',
debug=False,
strip=None,
upx=True,
console=True, icon='C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\AMOS.ico')

并将以下内容添加到我的 main.py 脚本中

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.path.abspath(".")

return os.path.join(base_path, relative_path)

Logo = resource_path("Logo.png")

关于python - Pyinstaller 和 --onefile : How to include an image in the exe file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31836104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com