1

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 )

2 Answers2

1

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"
  • On Windows, I had to change the colon in `"[workspace]/src/logo.png:."` to a semi-colon (`"[workspace]/src/logo.png;."`) – Sludge Jul 31 '22 at 20:16
0

Use this instead and then with pyinstaller convert main file to exe -

If this is the pixmap then add the function below and call it in pixmap

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

Add this function to your code it will work for labels, images etc

### 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)
graj499
  • 87
  • 2
  • 12