17

I'm having trouble importing a resource file. I'm using pyqt4 with monkey studio and I am trying to import a png image. When I run the program I get an import error like

ImportError: No module named icon_rc

I know that I have to compile it using pyrcc4 but I don't understand how to do this can anybody help please. It would be very helpful to have an answer that fully explains how to compile the resource file so I can import it.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Thomas
  • 1,199
  • 3
  • 13
  • 25

5 Answers5

30

Open cmd (or terminal on *nix) and run

pyrcc4 -py3 F:\computing\Payrollv22\icon.qrc -o icon_rc.py

It compiled the file successfully and I was able to import the py file into my project and run it with no problem.

CDspace
  • 2,639
  • 18
  • 30
  • 36
Thomas
  • 1,199
  • 3
  • 13
  • 25
  • 3
    It may be worth pointing out that the `-py3` switch is only required if you are using Python 3.x. If you are using Python 2.x, the resource file needs to be compiled without this additional switch. – 0x8BADF00D Feb 19 '15 at 12:31
  • 1
    @0x8BADF00D [According to the docs](http://pyqt.sourceforge.net/Docs/PyQt4/resources.html), the `-py3` flag will make a file that is compatible with any Python 2.6 or greater, so unless you're using a really archaic version of Python, you should always use `-py3` for future-proofing – jpyams Nov 03 '17 at 19:26
21

There really isn't much to explain here, you have a resource file (e.g. icon.qrc), then you call pyrcc4 -o icon_rc.py icon.qrc which will create a module icon_rc.py which you then can import in your project.

It's all documented here.

mata
  • 67,110
  • 10
  • 163
  • 162
  • i just cant convert this qrc file into a py module. when i use the code in cmd pyrcc4 -o icon_rc.py icon.qrc it says pyrcc4: File does not exist 'icon.qrc' – Thomas Apr 08 '13 at 12:38
  • And, does it exist in the directory wher you're running the command? – mata Apr 08 '13 at 13:41
  • 1
    ah okay so i managed to compile it and import it into my project but it now throws up a new error! File "", line 89, in File "F:\computing\Payrollv22\icon_rc.py", line 300, in qInitResources() File "F:\computing\Payrollv22\ritch_rc.py", line 295, in qInitResources QtCore.qRegisterResourceData(0x01, str(qt_resource_struct), str(qt_resource_name), str(qt_resource_data)) TypeError: qRegisterResourceData(int, str, str, str): argument 2 has unexpected type 'str' – Thomas Apr 08 '13 at 14:29
5

In Pyqt5 this command can be used Pyrcc5 input_file.qrc -o Out_file.py

We need to convert that qrc file into python file and then it can be imported to your code

IFfy KhAn
  • 71
  • 2
  • 7
2

its because when you also used pyuic5 to convert your UI to py, the resource file name from the UI sticks.

then use

Pyrcc5 input_file.qrc -o icons.py

remove from main_script.py

import icon_rc

and use

import icons

the when calling the actual icons from the icons module, you have to look at your qrc file prefix.

< RCC >
    < qresource
    prefix = "ico5" >
    < file > plugin.png < / file >
    < / qresource >
< / RCC >

if prefix is ico5 then you load icons with

 QtGui.QIcon(":/ico5/plugin.png")

and if prefix is , lets say,

<RCC>
    <qresource prefix="icons">

then its:

  QtGui.QIcon(":/icons/plugin.png")
Storm Shadow
  • 442
  • 5
  • 12
0

you could try with pyside as well like:

--- pyside-rcc -o input.qrc output.py

Nilesh K.
  • 371
  • 3
  • 4