Re: [PyQt] Python 3 import error

2010-12-03 Thread Thorsten Kampe
I just wanted to say that the amount of knowledgeable, fast help I 
received here yesterday was incredible. In this thread I experienced 
about six or so different problems porting a script to Python 3 and each 
one of that was immediately solved showing immense knowledge not just 
about PyQt itself but also about other Python related technologies.

Thanks, guys.

Thorsten

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


[PyQt] Python 3 import error

2010-12-02 Thread Thorsten Kampe
Hi,

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?!


Thorsten

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Phil Thompson
On Thu, 2 Dec 2010 12:24:54 +0100, Thorsten Kampe
thors...@thorstenkampe.de wrote:
 Hi,
 
 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?!

Make sure you regenerate resource_rc.py with the -py3 argument to pyrcc4.

Phil
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Thorsten Kampe
* Phil Thompson (Thu, 02 Dec 2010 11:31:13 +)
 On Thu, 2 Dec 2010 12:24:54 +0100, Thorsten Kampe
 thors...@thorstenkampe.de 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?!
 
 Make sure you regenerate resource_rc.py with the -py3 argument to pyrcc4.

Okay, I did this now but the error remained the same.

Thorsten

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Hans-Peter Jansen
On Thursday 02 December 2010, 12:24:54 Thorsten Kampe wrote:
 Hi,

 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..).

Pete
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Thorsten Kampe
* 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())

(...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 
:-/


Thorsten

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Phil Thompson
On Thu, 2 Dec 2010 13:17:22 +0100, Thorsten Kampe
thors...@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
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Thorsten Kampe
* Phil Thompson (Thu, 02 Dec 2010 12:39:58 +)
 On Thu, 2 Dec 2010 13:17:22 +0100, Thorsten Kampe
 thors...@thorstenkampe.de wrote:
  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.

Okay, if I omit the .toByteArray(), then the application starts with 
Python 3 - but it doesn't work with Python 2 anymore:

self.restoreGeometry(settings.value('Geometry'))
TypeError: QWidget.restoreGeometry(QByteArray): argument 1 has 
unexpected type 'QVariant'

Is there a way around that or do I simply have to query the Python 
version inside the script? Or put in a try without toByteArray() if 
exception, try with toByteArray()?!

The - hopefully - last problem with the porting to Python 3:
Everything seems to work fine, except closing the application through 
the menu or toolbar (Alt+F4 works fine):

Traceback (most recent call last):
  File my_application-new.pyw, line 32, in on_actionE_xit_triggered
settings.setValue('Geometry', QtCore.QVariant(self.saveGeometry()))
TypeError: PyQt4.QtCore.QVariant represents a mapped type and cannot be 
instantiated

this refers to 
def on_actionE_xit_triggered(self):
settings = QtCore.QSettings()
settings.setValue('Geometry', QtCore.QVariant(self.saveGeometry()))
self.close()

...in the code.

Thorsten

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Hans-Peter Jansen
On Thursday 02 December 2010, 18:31:04 Thorsten Kampe wrote:
 * Hans-Peter Jansen (Thu, 2 Dec 2010 15:47:28 +0100)

  On Thursday 02 December 2010, 14:25:21 Thorsten Kampe wrote:
   Okay, if I omit the .toByteArray(), then the application starts
   with Python 3 - but it doesn't work with Python 2 anymore:
  
   self.restoreGeometry(settings.value('Geometry'))
   TypeError: QWidget.restoreGeometry(QByteArray): argument 1 has
   unexpected type 'QVariant'
  
   Is there a way around that or do I simply have to query the
   Python version inside the script? Or put in a try without
   toByteArray() if exception, try with toByteArray()?!
 
  Add this before your _first_ PyQt4 import:
 
  # This is only needed for Python v2 but is harmless for Python v3.
  import sip
  sip.setapi('QVariant', 2)

 That works great, thanks.

   The - hopefully - last problem with the porting to Python 3:
   Everything seems to work fine, except closing the application
   through the menu or toolbar (Alt+F4 works fine):
  
   Traceback (most recent call last):
 File my_application-new.pyw, line 32, in
   on_actionE_xit_triggered settings.setValue('Geometry',
   QtCore.QVariant(self.saveGeometry())) TypeError:
   PyQt4.QtCore.QVariant represents a mapped type and cannot be
   instantiated
  
   this refers to
   def on_actionE_xit_triggered(self):
   settings = QtCore.QSettings()
   settings.setValue('Geometry',
   QtCore.QVariant(self.saveGeometry())) self.close()
 
  Please read my answer in this thread: Sip usage problem.

 I did that, but I don't fully understand - especially in regard to a
 solution. How do I have to modify
 settings.setValue('Geometry', QtCore.QVariant(self.saveGeometry()))
 ...so it doesn't throw the error anymore?

Omitting it should be sufficient:

settings.setValue('Geometry', self.saveGeometry())

Pete
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Thorsten Kampe
* Hans-Peter Jansen (Thu, 2 Dec 2010 20:16:13 +0100)
   sip.setapi('QVariant', 2)

Unfortunately, when I set sip.setapi('QVariant', 2) then I can't run 
the application as stand-alone Pyinstaller[1] executable (neither under 
Windows nor Linux):
ValueError: API 'QVariant' has already been set to version 1

Okay, this is not a PyQt issue but a Pyinstaller one (I think). But 
maybe you have a solution for that?


Thorsten
[1] http://www.pyinstaller.org/

___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Re: [PyQt] Python 3 import error

2010-12-02 Thread Nathan Weston

On 12/2/2010 5:11 PM, Thorsten Kampe wrote:

* Hans-Peter Jansen (Thu, 2 Dec 2010 20:16:13 +0100)

sip.setapi('QVariant', 2)


Unfortunately, when I set sip.setapi('QVariant', 2) then I can't run
the application as stand-alone Pyinstaller[1] executable (neither under
Windows nor Linux):
ValueError: API 'QVariant' has already been set to version 1



PyInstaller somehow ends up importing PyQt before your code runs, so the 
API version is implicitly set to 1. You can work around this by editing 
support/rthooks/pyi_rth_qt4plugins.py and adding these lines before the 
import of QtCore:


import sip
sip.setapi('QVariant', 2)

--
. . . . . . . . . . . . . . . . . . . . . . . . .
Nathan Weston   nat...@genarts.com
GenArts, Inc.   Tel: 617-492-2888
955 Mass. Ave   Fax: 617-492-2852
Cambridge, MA 02139 USA www.genarts.com
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt