Search code examples
pythonpyqtpy2exe

Problem while building a PyQt script with py2exe (QtCore)


I'm trying to make a simple hello-world executable python gui app in windows using pyqt. So I've made the pyqt.py file

import sys
from PyQt4.QtGui import *
app = QApplication(sys.argv)
button = QPushButton("Hello World", None)
button.show()
app.exec_()

I tried to use py2exe with the following setup.py script:

from py2exe.build_exe import py2exe
from distutils.core import setup

setup( console=[{"script": "pyqt.py"}] )

(I had the No module named sip error first, but it's solved thanks to the Py2exeAndPyQt page).

Now I have the executable and when i try to run it, I get the following error:

Traceback (most recent call last):
  File "pyqt.py", line 2, in <module>
  File "PyQt4\QtGui.pyc", line 12, in <module>
  File "PyQt4\QtGui.pyc", line 10, in __load
ImportError: No module named QtCore

How can I fix it? TIA


Solution

  • You can do something like this, you don't need import *.

    py2exe_opciones = {'py2exe': {"includes":["sip"]}}
    script = [{"script":"pyqt.py"}]
    
    setup(windows=script,options=py2exe_opciones)
    

    And now will the program should work. I had the same error.

    Here can read more.