Question

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

Was it helpful?

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.

OTHER TIPS

Add from PyQt4.QtCore import * to pyqt.py.

I'm not sure why it wasn't auto-included, but I think it has something to do with QtCore only being used by QtGui, which is a C++ lib... Like, py2exe only auto-detects python dependencies... So you have to import it manually.

this is an example

from setuptools import setup
import py2exe
from glob import glob

SETUP_DICT = {
    'windows': [{
        'script': 'main.py',
    }],

    'zipfile': 'lib/library.zip',

    'data_files': (
        ('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
        ('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
    ),

    'options': {
        'py2exe': {
            'bundle_files': 3,
            'includes': ['sip', 'PyQt4.QtCore'],
        },
    }
}

setup(**SETUP_DICT)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top