Ignore the import generated for icon(.qrc) file while converting the .ui file to .py using pyside2-uic
-
I have created a .ui file using qt5-designer. Also using the designer I created a .qrc file to store all the icons that I will be using throughout the app. The issue I am facing is that when I convert the .ui file to .py file using the following command
pyside2-uic appUI.ui -o appUI.py
, I also get an additional import of the icons file which isimport icons_rc
in the converted .py file.Is there an argument that I can use with
pyside2-uic
that will ignore that import because I want to keep the icons.qrc file in a separate location and import it from there in my main file. -
@PratikTayshete you can run pyside2-uic --help to get a list of arguments. I don't see anything to ignore rc, but --from-imports might be what you want? It'll use a relative path to the imported file.
-
@BamboozledBaboon I tried using the --from-imports argument but it is still adding an import given below:
The structure of my project is this:
And I am trying to import the icons.py file in my app.py file as given below:
When the execute the app.py file, it gives me this error:
-
@PratikTayshete
I don't havepyside2-uic
, but I don't see that--from-imports
does anything other than alterimport ...
tofrom . import ...
.There isn't a way to suppress importing
icons_rc
. Butpyside2-uic
is just some Python script itself, so easy to change. I look in https://github.com/pyside/pyside2-tools/blob/dev/pyside2-uic to see its source. Theimport
-writing function,def write_import()
, is at https://github.com/pyside/pyside2-tools/blob/f68388cf547c0d63a5d4a145f65aa9fa90529d52/pyside2uic/Compiler/misc.py#L27 (called from https://github.com/pyside/pyside2-tools/blob/f68388cf547c0d63a5d4a145f65aa9fa90529d52/pyside2uic/Compiler/compiler.py#L99). I'm sure you could tweak that to exclude youricons_rc
if required.Or, write a couple of lines script file to edit the
.py
file in-place to remove that line. -
Hi @PratikTayshete
I'm withuic
from PySide6 currently but some time ago my project was on PySide2 and it had a lot of ui-files at that time translated to python withpyside2-uic
.
What looks strange for me - this lineimport icons_rc
, how it appears in you py-file.
I.e. whatpyside2-uic
does - it translates one file format (ui) into another (py). And in my case there was no such line. It brings a thought into my head that your initial ui-file is somehow linked with qrc-resource file that your use. Is it true?
I.e. I see 2 options here:- your don't need resources in your ui-file but by some reason it contains it. Then you should clean your source ui-file (To be on a safe side you may create a brand new ui-file with a couple of simple widgets and pass it to uic in order to see if this line appear in output or not)
- you use resources in your ui-file, for example to have some icons in some places. In this case I belive it is some architectural restriction about how Qt expects your project should be sturctured. In this case probably more brainstrorming is needed about what you actually want to achieve.
-
@PratikTayshete
I'm too new to this to suggest a proper solution... but this seems to work for my for pyside6 to correctly import the _rc.py filepyside6-uic appUI.ui -o appUI.py --absolute-imports --python-paths ./icons/
Note: When I run pyside6-uic --help I see the following
--rc-prefix Python: Generate "rc_file" instead of "file_rc" import
This suggest to me that you are expected to name of your compiled resource python files to either prefix or suffix with rc_ or _rc respectively.