[PyQt] Relative imports in compiled UI files?

Virgil Dupras hsoft at hardcoded.net
Fri Aug 20 08:39:11 BST 2010


On Fri, Aug 20, 2010 at 8:45 AM, Virgil Dupras <hsoft at hardcoded.net> wrote:
> On Thu, Aug 19, 2010 at 1:23 PM, Phil Thompson
> <phil at riverbankcomputing.com> wrote:
>> On Tue, 17 Aug 2010 16:06:07 +0200, Virgil Dupras <hsoft at hardcoded.net>
>> wrote:
>>> I recently ported one of my apps to Python 3, and one of the headaches
>>> I had was because import statements in compiled UI files aren't
>>> relative ("from . import foo"). In python 2, it wasn't a problem
>>> because the lookup was made in the same folder as the module importing
>>> the thing, but under Python 3, I had to tweak my sys.path to make it
>>> work. If there was a way to generate relative imports, that would be a
>>> cleaner solution than my sys.path hackage.
>>>
>>> So, possible? If not, can I hope on it being implemented some day?
>>
>> Tonight's snapshot will have a --from-imports option that will do this for
>> the import of any resource file.
>>
>> Is that good enough, or were there other imports that were problematic?
>>
>> Phil
>>
>
> I use compileUiDir() and I don't see the argument in the list, is it
> yet-to-be-documented, or do I have to go through the command line?
>
> Virgil
>

I dug into the source and found the argument. For resource files, it
works as expected. There's the problem of custom widget imports (you
know, when in UI files we add custom classes and then promote widgets
to it) that don't get the "relative treatment", but I guess that if it
did, it would break some other code that imports widgets from
non-relative places. Anyway, I can change the unit name of my custom
widget and put a "." in front of it to make it work.

So yeah, it works for me, thanks.

Virgil


More information about the PyQt mailing list