Search code examples
pythonpyinstaller

Including an image in a PyInstaller onefile executable


I'm trying to make an executable with PyInstaller's onefile option. I've tried many, many different ways of fixing it and I'm absolutely stuck. Specifically, I tried Pyinstaller and --onefile: How to include an image in the exe file. I made my spec file look like theirs and included the resource_path() function. However, I get the following error:

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

So I'm not sure if my problem lies in the MEIPASS part, or maybe something with PIL. Another weird part of this is that before this error, my code includes:

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

Which produces no problem, even though both files (icon.ico, logo.png) are located in the same folder as the program that I turn into an executable. If it makes a difference, I'm running this on Mac.

The command I'm currently using:

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

and my spec file:

           # -*- 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 )

Solution

  • Okay well I was sure I had already tried this, but by adding the resource path method from Pyinstaller and --onefile: How to include an image in the exe file, but without changing the spec file, this command works:

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