17

I'm building qgis plugin and I can't find solution for this error.

File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 478, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named resources_napoved_rc

For everybody that will be asking I've built python resources file:

pyrcc4 -o resources_napoved_rc.py resources_napoved.qrc

I still can't find a way to make it work. I always get same error.

On top of the script I have:

import resources_napoved_rc.py

I'm using ui file directly from qtbuilder. Any ideas how to go forward ? I'm assuming this must be some kind of path problem or something similar.

3
  • No module named resources_napoved_rc and pyrcc4 -o resources_napoved_rc.py resources_napoved.qrc or is it a typing error ?
    – gene
    Commented Mar 19, 2015 at 21:09
  • No it's not a typo. I added rc suffix because it looks it is always looking for rc on the end of the file even if you pass file.qrc in UI file. (very strange) I tried all possible combinations also: pyrcc4 -o resources_napoved_rc.py resources_napoved_rc.qrc
    – Greg
    Commented Mar 20, 2015 at 2:44
  • 1
    Try to import it without the .py like this import resources_napoved_rc
    – Martin
    Commented Mar 20, 2015 at 8:32

11 Answers 11

23

This problem is caused by uic not working properly. I am not sure exactly why but I can show the symptoms and a workaround.

The initial plugin .ui file has an empty resources element:

<resources/>

When you edit the resources for the plugin in QtDesigner this changes to:

<resources>
  <include location="resources.qrc"/>
</resources>

This is the source of the problem. If you change the .qrc file in that include tag to resourcesXXX.qrc the error will change to No module named resourcesXXX_rc.

Note: the following is based upon a plugin build with 'test' entered into all fields of the QGIS plugin builder.

In the test_dialog.py file the following lines compile the .ui file:

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'test_dialog_base.ui'))

This is where the error is occurring.

Edit your .ui file to change back to <resources/> and the problem is resolved. Until you edit your dialog in QtDesigner again. You must make this edit to the .ui file after each time you edit your dialog.

The solution to this is to change your plugin to work like earlier versions of the plugin worked. This involves replacing the uic call and the class lines in the _dialog.py file. Replace these lines:

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'test_dialog_base.ui'))

class testDialog(QtGui.QDialog, FORM_CLASS):

with:

from test_dialog_base import Ui_testDialogBase

class testDialog(QDockWidget, Ui_testDialogBase):

You will now have to run

pyuic4 -x test_dialog_base.ui > test_dialog_base.py

when you first create your plugin and each time you edit your plugin dialog with QtDesigner. This was the old plugin method.

Whether to edit the .ui file or the run pyuic4 each time is your choice.

1
  • MaryBeth, the `resources element should be an empty element, not a closing tag of that element.
    – mariotomo
    Commented Jan 5, 2017 at 16:53
14

You can avoid having to edit the .ui file or compiling, etc by doing the following:

import sys
sys.path.append(os.path.dirname(__file__))
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'test_dialog_base.ui'), resource_suffix='')

resource_suffix defaults to '_rc' which is why it's always looking for 'resources_rc.' (you can always just leave it at default if you really want to use resources_rc, of course)

And appending the .ui file's path to sys.path allows python to find resources.py. You'd think it would be able to figure it out since your current directory is supposed to be in sys.path (and other imports from your current directory work), but it doesn't. I haven't looked into it enough to figure out exactly why.

12

I finally fixed it after 3 hours of trying. As someone said QT Designer adds

<resources> <include location="resources.qrc"/> </resources>

to your UI file. I simply deleted those rows and everything works fine (resources are visible in GUI)

1
  • 1
    It is still a problem with QGIS 2.14, so this and Worth Lutz answer helped a lot! Commented Sep 18, 2017 at 14:35
2

I think the short answer to this problem is that people are loading the project directly into QGIS instead of running:

make deploy

The file resources.py is only generated after the first time make deploy is run.

2

This is how I fixed the problem:

  • Run->cmd

  • Go to the location of your plugin builder (i.e: c:/users/rafik_pc/.qgis2/python/plugins/pluginbuilder)

  • type the following command:

    pyrcc4 -py2 resources.qrc -o resources.py
    

(I used -py2 because I'm using python 2.7)

  • restart your QGIS.

  • DONE.

1

You can give uic.loadUiType the package to import resources from, for example:

from pkg_resources import resource_filename
from qgis.PyQt import uic

FORM, BASE = uic.loadUiType(
    resource_filename('test', 'ui/ui_about.ui'),
    from_imports=True,
    import_from="test.ui",
)

I will make a pull request on QGIS Plugin builder when I'll get some time.

0

What worked for me on the end was to use original resources that came when I created package with plugin builder and use it in all other dialogs of my plugin. I still don't know why main icon of my plugin changes when reloading plugin, but still I get graphics in other dialogs.

I wish this would be different for many reasons. For building simple plugin this approach probably works. It's a organization thingie.

Since I had so many problems with this resources I've built resources.py and resources_rc.py that's probably why I get problems when reloading.

I don't have time to deal with this problem but QGIS dev team should put some attention to it since it blocks people to make beautiful plugins :D

What I ended up doing is to have simple:

import resources

before calling UI class.

Thanks for helping me with this mess.

PS: you have to "compile" your resources before adding them to your *.ui file and use use same resources -> import resources in all your plugin dialogs. I hope this helps someone.

0

When you create your QGIS plugin with this process, you are creating a compile.bat file containing the following :

@echo off
call "C:\OSGeo4W64\bin\o4w_env.bat"
call "C:\OSGeo4W64\bin\qt5_env.bat"
call "C:\OSGeo4W64\bin\py3_env.bat"

@echo on
pyrcc5 -o resources.py resources.qrc

I can just execute it.

EDIT 1 :

And do not forget to check that your resources.qrc file has the right prefix attribute in the qresource tag. For my case it is something like :

<qresource prefix="/plugins/plugin_name">

EDIT 2 :

Delete the rows with the tag resources in the .ui file.

0

Instructions for those who use QGIS installed with OSGeo4W. I was able to update the resources.py file by following these steps:

  1. Open OSGeo4W Shell (typical location: C:\OSGeo4W\OSGeo4W.bat)
  2. Type pyrcc5 "...\plugin_folder_name\resources.qrc" -o "...\plugin_folder_name\resources.py"
0

To complement Worth Lutz answer, you can replace " <include...>" from <your_base>.ui file editing __init__.py file class factory:

import os

def classFactory(iface):
    file_dir = os.path.dirname(__file__)
    with open(os.path.join(file_dir, "your_base.ui"), 'r') as file:
        ui_content = file.read()
    ui_content = ui_content.replace('<include location="resources.qrc"/>', '')
    with open(os.path.join(file_dir, "your_base.ui"), 'w') as file:
        file.write(ui_content)

    from .your_plugin import YourPlugin
    return YourPlugin(iface)
-1

Execute the same command using

sudo pyrcc4 -o resources_rc.py resources.qrc

It will work

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.