简体   繁体   中英

PyQt and py2exe or cx_freeze: AttributeError

Here I have Python 2.7.3 (x64), PyQt 4.9.5-1 (x64) running on Win7 x64. I want to convert a simple PyQt script to an exe file.
This is my python script:

#!/usr/bin/env python

import sys
from PyQt4 import Qt

a = Qt.QApplication(sys.argv)

def sayHello():
    print "Hello, World!"

hellobutton = Qt.QPushButton("Say 'Hello world!'",None)

a.connect(hellobutton, Qt.SIGNAL("clicked()"), sayHello)

hellobutton.show()
a.exec_()

Running it from the command line works as expected. I use a setup.py for py2exe:

from distutils.core import setup
import py2exe
setup(console=['pyqt-example.py'])

However, if I try to convert it to an exe file with py2exe 0.6.9 with python setup.py py2exe , I get this error when running the exe file:

Traceback (most recent call last):
  File "pyqt-example.py", line 6, in <module>
    a = Qt.QApplication(sys.argv)
AttributeError: 'module' object has no attribute 'QApplication'

I also tried cx_freeze 4.3 with \\Python27\\Scripts\\cxfreeze pyqt-example.py --target-dir dist . This results in:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec code in m.__dict__
  File "pyqt-example.py", line 6, in <module>
    a = Qt.QApplication(sys.argv)
AttributeError: 'module' object has no attribute 'QApplication'

So I assume that I missed to inform both of these tools about the location of some Qt components. What exactly am I missing?

Thanks Avaris, your hint was correct. Avoiding the use of module Qt is the answer. This is the script which works fine:

#!/usr/bin/env python

import sys
from PyQt4 import QtGui,QtCore

a = QtGui.QApplication(sys.argv)

def sayHello():
    print "Hello, World!"

hellobutton = QtGui.QPushButton("Say 'Hello world!'",None)

a.connect(hellobutton, QtCore.SIGNAL("clicked()"), sayHello)

hellobutton.show()
a.exec_()

After that, I have to call cx_freeze so: cxfreeze pyqt-example.py --include-modules atexit --target-dir dist . And it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM