I have constructed a QGIS plugin using QGIS Plugin Builder 3.0.3.

Within my plugin, I need to load a .png file as a Qicon object. What needs to be done for the plugin script to locate the .png file when it is run within QGIS?

For my situation, my .png file is called line_1x.png.

I have modified the resources.qrc within the plugin to show a reference to the icon as shown below:

<qresource prefix="/plugins/geomAttribute" >

I compiled the resources file using the following command pyrcc5 -o resources.py resources.qrc.

Within my plugin python script I try to construct the QIcon object using: icon = QIcon('./line_1x.png')

However, the plugin python script can't access the line_1x.png file. When I use a full file path to the icon file instead of a relative path the icon image file is read properly. Hence, using the following does work: icon = QIcon('C:/Users/pippi/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins/geomattribute/line_1x.png'

How do a I get a QGIS plugin to read an image for an icon using a relative path?

3 Answers 3


The relative path for a file in a plugin is:


If in doubt, search for the path used for the icon_path in the def initGui(self): method.


It is also possible to construct the path with:

self.plugin_dir + '/icon.png'

where self is the plugin class.


you can try:

import os

QIcon(os.path.dirname(__file__) + "/line_1x.png")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.