Problem with Python 2.6 and PyQt4 on Windows

6,024 views
Skip to first unread message

Nathan Weston

unread,
Oct 8, 2010, 11:17:20 AM10/8/10
to PyInstaller
I'm trying to package a trivial PyQt app with Python 2.6 on Windows.
As recommended on the Wiki, I'm using the trunk (r897) for WinSxS
support.

My exe builds without errors, but when run it can't find PyQt:

Traceback (most recent call last):
File "<string>", line 14, in <module>
File "C:\home\nathan\src\pyinstaller-trunk-r897\iu.py", line 455, in
importHook
raise ImportError, "No module named %s" % fqname
ImportError: No module named PyQt4.QtCore

The warnings file generated by Build.py includes some Qt-related
messages:
W: no module named PyQt4._qt (top-level import by PyQt4.QtGui)
W: no module named readline (delayed import by pdb)
W: no module named pwd (delayed, conditional import by posixpath)
W: no module named org (top-level import by pickle)
W: no module named posix (delayed, conditional import by iu)
W: no module named PyQt4._qt (top-level import by PyQt4)
W: no module named PyQt4._qt (top-level import by PyQt4.QtCore)

Is this expected to work on the trunk, or should I be using some other
snapshot?

Giovanni Bajo

unread,
Oct 8, 2010, 12:17:35 PM10/8/10
to pyins...@googlegroups.com

No, this is supposed to work. Are you using the standard GPL installer
of PyQt from Riverbank's site or have you built PyQt yourself?
--
Giovanni Bajo :: ra...@develer.com
Develer S.r.l. :: http://www.develer.com


Nathan Weston

unread,
Oct 8, 2010, 12:40:23 PM10/8/10
to pyins...@googlegroups.com

I'm using the Riverbank installer: PyQt4 GPL v4.7.4 for Python 2.6. I'm
running Windows 7 64-bit, but Python and PyQt are 32-bit.

My program looks like this:
# qthello.py
import sys
import sip
import PyQt4.QtCore
import PyQt4.QtGui

app = PyQt4.QtGui.QApplication(sys.argv)
window = PyQt4.QtGui.QMainWindow()
window.setCentralWidget(PyQt4.QtGui.QLabel("Hello"))
window.show()
sys.exit(app.exec_())
# end of qthello.py

I built it like so:
python Makespec.py qthello.py
python Build.py qthello\qthello.spec

Let me know if there's any other information I can provide.

Raoul Snyman

unread,
Oct 8, 2010, 3:47:38 PM10/8/10
to pyins...@googlegroups.com
On 8 October 2010 18:40, Nathan Weston <elb...@spamcop.net> wrote:
> My program looks like this:
> # qthello.py
> import sys
> import sip
> import PyQt4.QtCore
> import PyQt4.QtGui
>
> app = PyQt4.QtGui.QApplication(sys.argv)
> window = PyQt4.QtGui.QMainWindow()
> window.setCentralWidget(PyQt4.QtGui.QLabel("Hello"))
> window.show()
> sys.exit(app.exec_())
> # end of qthello.py

Your application is incorrect. You have to import PyQt4 like so:

from PyQt4 import QtCore, QtGui

Also, there is no need to import sip.

--
Raoul Snyman
B.Tech Information Technology (Software Engineering)
E-Mail:   raoul....@gmail.com
Web:      http://www.saturnlaboratories.co.za/
Blog:      http://blog.saturnlaboratories.co.za/
Mobile:   082 550 3754
Registered Linux User #333298 (http://counter.li.org)

Nathan Weston

unread,
Oct 8, 2010, 4:10:11 PM10/8/10
to pyins...@googlegroups.com
On 10/8/2010 3:47 PM, Raoul Snyman wrote:
> On 8 October 2010 18:40, Nathan Weston<elb...@spamcop.net> wrote:
>> My program looks like this:
>> # qthello.py
>> import sys
>> import sip
>> import PyQt4.QtCore
>> import PyQt4.QtGui
>>
>> app = PyQt4.QtGui.QApplication(sys.argv)
>> window = PyQt4.QtGui.QMainWindow()
>> window.setCentralWidget(PyQt4.QtGui.QLabel("Hello"))
>> window.show()
>> sys.exit(app.exec_())
>> # end of qthello.py
>
> Your application is incorrect. You have to import PyQt4 like so:
>
> from PyQt4 import QtCore, QtGui

Thanks, that fixes the problem. But it seems like a stretch to call the
program "incorrect" when it works just fine. Why does PyInstaller
understand one form of import but not the other?

> Also, there is no need to import sip.
>

True. In my real program I'm importing sip to do this:

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
from PyQt4.QtGui import *

Which leads me to another problem. When I package the larger program
with PyInstaller and run the EXE, I get this error:

ValueError: API 'QString' has already been set to version 1

It seems as though PyQt is somehow getting loaded before my sip.setapi
calls, even though I haven't explicitly imported it yet. Again, it works
correctly when I run the .py file directly. Is there a workaround for this?

- Nathan

Martin Zibricky

unread,
Oct 8, 2010, 5:20:53 PM10/8/10
to pyins...@googlegroups.com
Nathan Weston píše v Pá 08. 10. 2010 v 16:10 -0400:

> it works
> correctly when I run the .py file directly. Is there a workaround for
> this?

Yes, it is.

put the following lines somewhere at the top in
file ./trunk/support/rthooks/pyi_rth_qt4plugins.py

Giovanni Bajo

unread,
Oct 9, 2010, 6:47:43 AM10/9/10
to pyins...@googlegroups.com
On 10/8/2010 10:10 PM, Nathan Weston wrote:
> On 10/8/2010 3:47 PM, Raoul Snyman wrote:
>> On 8 October 2010 18:40, Nathan Weston<elb...@spamcop.net> wrote:
>>> My program looks like this:
>>> # qthello.py
>>> import sys
>>> import sip
>>> import PyQt4.QtCore
>>> import PyQt4.QtGui
>>>
>>> app = PyQt4.QtGui.QApplication(sys.argv)
>>> window = PyQt4.QtGui.QMainWindow()
>>> window.setCentralWidget(PyQt4.QtGui.QLabel("Hello"))
>>> window.show()
>>> sys.exit(app.exec_())
>>> # end of qthello.py
>>
>> Your application is incorrect. You have to import PyQt4 like so:
>>
>> from PyQt4 import QtCore, QtGui
>
> Thanks, that fixes the problem. But it seems like a stretch to call the
> program "incorrect" when it works just fine. Why does PyInstaller
> understand one form of import but not the other?

No reason, actually, sounds like a bug (though it's hard to believe
since it's a pretty common form of imports). I'll have a look.

Reply all
Reply to author
Forward
0 new messages