11

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.

  • No module named resources_napoved_rc and pyrcc4 -o resources_napoved_rc.py resources_napoved.qrc or is it a typing error ? – gene Mar 19 '15 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 Mar 20 '15 at 2:44
  • Try to import it without the .py like this import resources_napoved_rc – Martin Mar 20 '15 at 8:32
16

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.

  • MaryBeth, the `resources element should be an empty element, not a closing tag of that element. – mariotomo Jan 5 '17 at 16:53
8

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)

  • It is still a problem with QGIS 2.14, so this and Worth Lutz answer helped a lot! – Andreas Müller Sep 18 '17 at 14:35
7

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.

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.

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

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, privacy policy and cookie policy

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