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

created at 12-29-2021 views: 9


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.

    <qresource prefix="/">

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.qrc

The generated 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"\

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

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


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

import images


Note that after using the resource reference method of, 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

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, try to have only one .py file in the same level directory of, otherwise it is easy to find the reference package when packaged into an exe file.

created at:12-29-2021
edited at: 12-29-2021: