简体   繁体   中英

Executable from Python2 script w. PyInstaller: Qt4 module import error

I have a project with a GUI created in Qt Designer and compiled with pyuic . So I import QtCore and QtGui from PyQt4.

The script runs OK .

I need an executable file. The tool is PyInstaller, target platforms - Linux and Windows. I've tried and had a success once. Then I developed a project for some time and now... I cannot make an executable - it crashes with

ImportError: No module named QtCore

The problem is that I cannot compare current project with the old one. And I'm not sure how the environment in my PC has changed.

So I have to understand why PyInstaller makes an executable with no error messages - but the program crashes. Or how to help PyInstaller (I've read manual and tried a lot from it but to no avail).

Here is a simplified version of my project (actually a single file) that has the main feature: it runs OK from Python and crashes as standalone program.

#!/usr/bin/python
# -*- coding: utf-8 -*-
""" test script to learn PyInstaller usage
"""
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui


class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_ADCmonitor()
        self.ui.setupUi(self)
        self.exitAction = QtGui.QAction('Exit', self)
        self.exitAction.setShortcut('Ctrl+Q')
        self.exitAction.triggered.connect(self.close)
        toolbar = self.addToolBar('Exit')
        toolbar.addAction(self.exitAction)
        self.refresh = QtCore.QTimer()
        self.status_freeze_timer = QtCore.QTimer()
        self.update_data()

    def update_data(self):
        self.status_refresh('Ok')
        self.refresh.singleShot(200, self.update_data)

    def status_refresh(self, msg):
        self.ui.statusbar.showMessage(msg)


class Ui_ADCmonitor(object):
    def setupUi(self, ADCmonitor):
        ADCmonitor.setObjectName("ADCmonitor")
        ADCmonitor.resize(300, 300)
        self.statusbar = QtGui.QStatusBar(ADCmonitor)
        self.statusbar.setObjectName("statusbar")
        ADCmonitor.setStatusBar(self.statusbar)
        QtCore.QMetaObject.connectSlotsByName(ADCmonitor)
        ADCmonitor.setWindowTitle("ADC Monitor")


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())

And please don't propose me to use other dist utilities. I tried some of them, I've chosen PyInstaller and I'd like to use it.

So, the problem is in a PyInstaller 2.1 bug - as Joran Beasley supposed.

Solution:

sudo pip install git+ https://github.com/pyinstaller/pyinstaller.git

And bingo!

pyinstaller myscript.py makes correct exec.

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