Customize pyuic's resource import statement?
Asked Answered
B

2

8

When I use this command on windows:

python -m PyQt4.uic.pyuic user_interface.ui -o user_interface.py

After that, I add a resource:

pyrcc4.exe -py3 images.qrc -o images.py

And I end up with two beautiful files, user_interface.py, and images.py. The problem is that the user_interface.py file ends with this line of code:

... all QT stuff here.
import images_re

And because this is a module that is called from many parents, it has to be imported like this:

import myapp.gui.images_re

When I change the line of code it works perfectly, but every time I modify the user_interface.ui file and then execute the batch, it will be overwritten, and I will have to change it manually every time.

Is there any way to tell pyuic what to write in that import statement?
Or any batch code that can be executed after the pyuic and changes that line of code?
Or some tweak on the .py file that calls user_interface.py for example to change the default directory so it imports images_re from there?

Burglarize answered 22/11, 2015 at 22:41 Comment(3)
--from-imports.Gowan
Thank you, it worked perfectly. Post it as an answer and you got it.Burglarize
If you want to provide a more useful answer, you could tell me the shell code I have to use to convert ui to py using that argument, because I tried pyuic.bat file.ui --from-imports "myapp.gui" --output file.py and it says error: one input file must be specified.Burglarize
G
12

If you save the resource file in the same package directory as the ui file, then you can use the --from_imports option. This will add the following import line to the ui file:

    from . import resources_rc

And the command would look something like this:

    pyuic4 --from-imports --output file.py file.ui

(NB: the pyuic executable name may differ, depending on the platform).

Gowan answered 23/11, 2015 at 3:32 Comment(0)
L
0

If you use pyuic4 -h, you can use the --import-from=PACKAGE option.

When you build the UI file, you should type:

pyuic user_interface.ui -o user_interface.py --import-from=myapp.gui

It worked for me.

Lovage answered 9/12, 2021 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.