0

I use PyQt5 to develop a GUI application in python, and that works. Now, I would like to generate exe file using PyInstaller, but I don't know how to include image files into exe file. I mean I wish the exe file doesn't rely on external image files any more. I have tried the method below, but it seems doesn't work for me. Pyinstaller and --onefile: How to include an image in the exe file

following is part of my code:

my code:

class DASH(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.animation = Animation()
        self.setCentralWidget(self.animation)
        exitAct = QAction(QIcon(r'car.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(self.close)

spec file:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['C:\\Jiawei\\Work\\python_simulation_V2\\python_visualization.py'],
         pathex=['C:\\Users\\Lujia'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

a.datas += [('car.png','C:\\Jiawei\\Work\\python_simulation_V2\\images\\car.png', 'Data')]

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      [],
      name='python_visualization',
      debug=False,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      runtime_tmpdir=None,
      console=True )
Jiawei Lu
  • 509
  • 6
  • 16
  • In what way doesn’t it work - have you examined the directory where your onefile exe is extracted to and the image file isn’t there? – DisappointedByUnaccountableMod Jan 10 '19 at 08:37
  • @barny my understanding of line a.datas += .... is that PyInstaller will find the picture I stored in 'C:\\Jiawei\\Work\\python_simulation_V2\\images\\car.png', and save it as an object named 'car.png' when it builds exe files. In this way, I can refer to the picture using 'car.png' in my main script. But when I run the generated exe file, it doesn't show the picture in GUI. I'm not sure where is wrong – Jiawei Lu Jan 13 '19 at 21:25
  • If your code can’t find at runtime that doesn’t necessarily mean the file isn’t there, it’s just not where your code expects it to be. I repeat: have you checked the temporary directory where the onefile exe is extracted to, to find where the car.png is located? Also, when the exe is run, check the current working directory because that will most likely not be the same folder as the temporary directory. – DisappointedByUnaccountableMod Jan 13 '19 at 21:31
  • Your `a.datas` specification puts the file in the `Data` folder - is that where your code is looking? – DisappointedByUnaccountableMod Jan 13 '19 at 21:34

0 Answers0