PyInstaller won't load the PyQt's images to the GUI
Asked Answered
S

4

12

I've been having some complications to pass my script into an executable, but I finally managed to. The main problem is that PyInstaller doesn't load the images to the GUI.

This is how it should look like:

How it should look like

This is how it looks like:

How it looks like

And I can't seem to find the problem, this is the .spec file:

a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), os.path.join(HOMEPATH,'support\\useUnicode.py'), 'programa.py'],
             pathex=['img', 'C:\\Users\\Poblet\\ManGet\\HeyMang\\pyinstaller'])
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=1,
          name=os.path.join('build\\pyi.win32\\Hey Mang!', 'Hey Mang!.exe'),
          debug=False,
          icon='heymang.ico',
          strip=False,
          upx=True,
          console=False )
coll = COLLECT( exe,
               Tree('C:\\Users\\Poblet\\ManGet\\HeyMang\\pyinstaller\\img'),
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name=os.path.join('dist', 'Hey Mang!'))
app = BUNDLE(coll,
             name=os.path.join('dist', 'Hey Mang!.app'))

And it supposes to grab those images, like it says here:

Warnings written to C:\Users\Poblet\ManGet\HeyMang\pyinstaller\Hey Mang!\warnHey Mang!.txt
checking PYZ
rebuilding outPYZ1.toc because outPYZ1.pyz is missing
building PYZ outPYZ1.toc
checking PKG
rebuilding outPKG3.toc because outPKG3.pkg is missing
building PKG outPKG3.pkg
checking EXE
rebuilding outEXE2.toc because Hey Mang!.exe missing
building EXE from outEXE2.toc
I: SRCPATH [('heymang.ico', None)]
I: Updating icons from ['heymang.ico'] to c:\users\poblet\appdata\local\temp\tmpr34zmp
I: Writing RT_GROUP_ICON 0 resource with 76 bytes
I: Writing RT_ICON 1 resource with 1128 bytes
I: Writing RT_ICON 2 resource with 4264 bytes
I: Writing RT_ICON 3 resource with 9640 bytes
I: Writing RT_ICON 4 resource with 16936 bytes
I: Writing RT_ICON 5 resource with 67624 bytes

And they are in the folder, but they won't work for a reason or another.

The entire source code (minus the PyInstaller files) is here.

I appreciate your help.

Scot answered 18/7, 2012 at 4:26 Comment(1)
Not a direct solution to your problem but you can try the Qt Resource System and pyrcc4 to convert them into .py files. I had no troubles in PyQt scripts with resource files compiled using PyInstaller.Leucas
S
20

I was able to solve this, and this should help others as well:

  • Create the .spec file with the following command:

    python Makespec.py --noconsole --icon="youricon.ico" --name="App name" program.py
    
  • Open the .spec file (eg.: App name/App name.spec) and you should see something like this:

    a = Analysis([
            os.path.join(HOMEPATH,'support\\_mountzlib.py'),
            os.path.join(HOMEPATH,'support\\useUnicode.py'),
            'program.py'
        ], pathex=[
            'C:\\Your\\User\\Path\\To\\pyinstaller'
    ])
    pyz = PYZ(a.pure)
    exe = EXE(
            pyz,
            a.scripts,
            exclude_binaries=1,
            name=os.path.join('build\\pyi.win32\\App Name', 'App Name.exe'),
            debug=False,
            strip=False,
            upx=True,
            console=False , icon='youricon.ico'
    )
    coll = COLLECT(
            exe,
            a.binaries,
            a.zipfiles,
            a.datas,
            strip=False,
            upx=True,
            name=os.path.join('dist', 'Hey Mang!')
    )
    app = BUNDLE(coll, name=os.path.join('dist', 'Hey Mang!.app'))
    

    And before a.binaries you should add this piece of code:

           Tree('C:\\Your\\App\\Path\\To\\Images'),
    

    So when PyInstaller reads the .spec file, the compiler will pass the image to the dist directory.

  • Now we need to create the .qrc file, which will load our images. And this file should look something like this:

    <RCC>
      <qresource prefix="/" >
        <file>img/image1.png</file>
        <file>img/image2.png</file>
        <file>img/image3.png</file>
      </qresource>
    </RCC>
    

    With your images, obviously. And this needs to be compiled to .py format, with the following command:

    pyrcc4 -o images.qrc images_qr.py
    
  • And finally, we need to add this to our script, for example like this:

    import images_qr
    
    ...
    
    self.setWindowIcon(QtGui.QIcon(':/img/image1.png')) # The colon must be there
    

And once you compile you should see the images just fine, like this:

I hope this helps everyone with the same issue. Remember to give the proper image paths and to add the colon to your images.

Scot answered 18/7, 2012 at 17:36 Comment(6)
Thanks Leandro! I spent AGES trying to get my icon working... Pyinstaller is new to me, and I think I would have been here all night if you hadn't written this explanation. All the best.Primalia
Wow, this could be the problem I've been having all this time. Thank you! Though... saying that, using a combination of Python and Qt seems to add these few extra steps onto the build process. I'm swaying towards using wxWidgets, which seems simpler and doesn't require resources to be compiled, as far as I know.Energumen
BTW - see my post above. The Tree command in the spec file is not necessary for this to work. Just create the resource files.Tager
You may be able to help solve my problem.Atrice
I know this is a 4 year old question, but if anyone could answer this. Does this method work for importing any images to the dist directory or just the icon. Because I'm using pygame with pyinstaller and I can't seem to make the images go to the proper directory.Czardom
What is Makespec.py?Zigmund
T
3

A quick update to the method below. First off - this is a great way to easily include images in a PyQt/Pyside app. For Pyside simply use pyside-rcc.exe rather than pyrcc4.

Secondly, I'm not convinced that the Tree(...) command is even necessary. After creating a dist folder with this method, I removed all the image files that were copied in with the Tree.. command. My executable still works and the images are displayed correctly. So it may be that we simply need to create the qrc file and run the pyrcc program, making sure that the python modules are updated with the colon prefix to iamge file paths.

Tager answered 29/4, 2014 at 15:44 Comment(1)
Just tried building without the Tree commands in the spec file and it works fine.Tager
O
3

If you are using Qt Designer, you can add icons and images without much difficulty. Follow the tutorial here http://doc.qt.io/qt-4.8/designer-resources.html

Tested on PySide 1.2.2 with Qt Designer 4.8.5 and Pyinstaller 2.1

Osmanli answered 8/8, 2015 at 17:46 Comment(1)
You may be able to help solve my problem.Atrice
S
0

Move exe file to folder where image folder is located or in the same folder where the main.py is.

See this

Selfmortification answered 27/1, 2020 at 7:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.