[PyQt] Python 3 import error
Phil Thompson
phil at riverbankcomputing.com
Thu Dec 2 12:39:58 GMT 2010
On Thu, 2 Dec 2010 13:17:22 +0100, Thorsten Kampe
<thorsten at thorstenkampe.de> wrote:
> * Hans-Peter Jansen (Thu, 2 Dec 2010 12:49:17 +0100)
>> On Thursday 02 December 2010, 12:24:54 Thorsten Kampe wrote:
>> > I'm trying to port a PyQt4 application from Python 2 to Python 3.
>> > The application works fine with the latest Python 2 (2.7.1) but not
>> > with Python 3.1.2:
>> >
>> > Traceback (most recent call last):
>> > File "my_application.pyw", line 10, in <module>
>> > import resource.ui
>> > File "F:\[path]\resource\ui.py", line 84, in <module>
>> > import resource_rc
>> > ImportError: No module named resource_rc
>> >
>> > This is the directory structure:
>> > |-- my_application.pyw* (main application, "import resource.ui")
>> >
>> > `-- resource/
>> >
>> > |-- __init__.py (package file to enable module import)
>> > |-- resource_rc.py (generated by pyrcc4)
>> >
>> > `-- ui.py (generated by pyuic4, "import resource_rc")
>> >
>> > I couldn't find any documentation regarding changes between the
>> > latest Python 2 and 3. Who knows what's going on and what I can do?!
>>
>> Did you recreated the resources with pyrcc4 -py3? It might be necessary
>> to supply a --from-imports argument to pyuic4, as python3 did changed
>> its import behavior (relative imports, etc..).
>
> Okay, this is what I get after adding "--from-imports":
>
> Traceback (most recent call last):
> File "my_application.pyw", line 10, in <module>
> import resource.ui
> File "F:\[path]\resource\ui.py", line 89, in <module>
> from . import resource_rc
> File "F:\[path]\resource\resource_rc.py", line 70627,
> in <module>
> qInitResources()
> File "F:\[path]\resource\resource_rc.py", line 70622,
> in qInitResources
> QtCore.qRegisterResourceData(0x01, qt_resource_struct,
> qt_resource_name, qt_resource_data)
> TypeError: qRegisterResourceData(int, str, str, str): argument 2 has
> unexpected type 'str'
>
> So the ImportError is gone. Adding -py3 to pyrcc4 yields:
> Traceback (most recent call last):
> File "my_application.pyw", line 63, in <module>
> mainWin = MainWindow()
> File "my_application.pyw", line 19, in __init__
> self.restoreGeometry(settings.value('Geometry').toByteArray())
> AttributeError: 'QByteArray' object has no attribute 'toByteArray'
>
> So the error is now in the main file. The actual line is
>
> self.restoreGeometry(settings.value('Geometry').toByteArray())
...because the QVariant v2 API is the default with Python v3 so it has
already converted the QVariant to a QByteArray for you.
> (...taken from Mark Summerfield's book "Rapid GUI Programming with
> Python and Qt: The Definitive Guide to PyQt Programming").
>
> Interestingly the same UI and resource file still run fine in Python 2
> :-/
.ui files are identical for all versions of Python.
-py3 resource files will work with Python v2.6 and later.
Phil
More information about the PyQt
mailing list