在PyInstaller onefile可执行文件中包含映像



我正试图用PyInstaller的onefile选项制作一个可执行文件。我尝试了很多不同的方法来修复它,但我完全被卡住了。具体来说,我试过Pyinstaller和--onefile:如何在exe文件中包含图像。我让我的规范文件看起来像他们的,并包含了resource_path((函数。然而,我得到以下错误:

File "PIL/Image.py", line 2953, in open
FileNotFoundError: [Errno 2] No such file or directory'/var/folders/t5/vkb5xkjs3517p5jlfkbj89vm0000gp/T/_MEIdOLb1O/logo.png'

所以我不确定我的问题是在MEIPASS部分,还是PIL的问题。另一个奇怪的部分是,在这个错误之前,我的代码包括:

self.iconbitmap(r"[workspace]/src/icon.ico")

即使这两个文件(icon.ico、logo.png(与我转换为可执行文件的程序位于同一文件夹中,也不会产生任何问题。如果有什么不同的话,我会在Mac上运行这个。

我当前使用的命令:

pyinstaller --noconfirm --onefile --console "[workspace]/src/gui.py" gui.spec  

和我的规范文件:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['gui.py'],
pathex=['/Users/Freddie/Impruvon/guiwebscraperproject/venv/src'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
for d in a.datas:
if 'pyconfig' in d[0]:
a.datas.remove(d)
break
a.datas += [('logo.png','/Users/Freddie/Impruvon/guiwebscraperproject/venv/src/logo.png', 'Data')]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,  
[],
name='gui',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )

好吧,我确信我已经尝试过了,但通过添加Pyinstaller的资源路径方法和--onefile:如何在exe文件中包括图像,但不更改规范文件,这个命令有效:

pyinstaller --noconfirm --onefile --console --add-data "[workspace]/src/logo.png:."  "[workspace]/src/gui.py"

使用此选项,然后使用pyinstaller将主文件转换为exe-

如果这是pixmap,则添加下面的函数并在pixmap中调用它

self.label.setPixmap(
QtGui.QPixmap(
self.resource_path("MyPNG_FILE.png")
)
)

将此函数添加到您的代码中,它将适用于标签、图像等

### Use this function To attach files to the exe file (eg - png, txt, jpg etc) using pyinstaller
def resource_path(self, relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)

最新更新