Solve the problem that external resources cannot be loaded during the packaging process of pyinstaller

created at 12-29-2021 views: 3

Introduction

Record here how we can pack external resource files into exe applications, so that no matter which computer it is executed on, we can display our resource pictures, and there will be no resources not found.

First of all, it is a resource file in QRC format. Configure the resource path to such a file as shown below. I created a file called images.qrc.

<RCC>
    <qresource prefix="/">
        <file>default.png</file>
        <file>images/1.jpg</file>
        <file>images/2.jpg</file>
    </qresource>
</RCC>

Configure the path of the resource file to be packaged as above.

Note that the resource path configuration file (images.qrc) here is in the same directory as the .py file you need to package.

The next step is to generate the python module file of .py from qrc. In fact, after the generation, the external resource is converted into a binary file. Use the following command to generate it.

pyrcc5 -o images.py images.qrc

The generated images.py file is just like this, because it is too long, only part of the content is shown here.

# -*- coding: utf-8 -*-

# Resource object code
#
# Created by: The Resource Compiler for PyQt5 (Qt v5.15.2)
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore

qt_resource_data = b"\
\x00\x01\x14\x4d\
\x89\
\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\
\x00\x01\x44\x00\x00\x01\x41\x08\x06\x00\x00\x00\x0f\x27\xd6\x75\
\x00\x00\x00\x01\x73\x52\x47\x42\x00\xae\xce\x1c\xe9\x00\x00\x00\
\x04\x67\x41\x4d\x41\x00\x00\xb1\x8f\x0b\xfc\x61\x05\x00\x00\x00\
\x09\x70\x48\x59\x73\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7\x6f\
\xa8\x64\x00\x00\xff\xa5\x49\x44\x41\x54\x78\x5e\xec\xbd\x07\xbc\
\x56\xc5\x96\xe6\x3d\xf3\x4d\xce\x3d\x33\x3d\xb7\xbb\x67\xfa\x76\
\x98\xee\xb9\xdd\xf7\x5e\xf5\x9a\x23\x8a\x59\x89\x06\x10\x73\x44\

Finally, we need to import the resource file images.py module we generated into the module that needs to be referenced.

Before processing, our original way of referencing resources is like this.

self.image.setPixmap(QPixmap('default.png'))

After processing, you need to import the previously converted images.py module to the current module that needs to reference resources.

import images

self.image.setPixmap(QPixmap(':/default.png'))

Note that after using the resource reference method of images.py, a colon is used in front of the image path.

Finally, perform the normal packaging process. After the packaging is completed, we will find that our resource pictures can be referenced normally.

pyinstaller -F -w -i favicon.ico test2.py

You can use the -i parameter to add an icon to the application.

If there is a problem that the module did not find during the packaging process, put the referenced custom module under the external package for reference. For example, if you need to package main.py, try to have only one .py file in the same level directory of main.py, otherwise it is easy to find the reference package when packaged into an exe file.

Please log in to leave a comment.