Deploying a PyQt app using py2exe

For future reference this exercise was done on Windows 8.1 with Python(x,y) and MS C++ 2008 Redistributable Package installed (though py2exe still couldn’t find MSVCR90.DLL):

In the same folder have three files like in the TestPyQtGUIApp archive.

The MyGUIApp.py contents is:

"""
Created on Sat Mar 05 11:37:02 2016

@author: e1z
"""

import sys

from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtCore import SIGNAL, SLOT

class MyGUIApp(QtGui.QApplication):
  def __init__(self):
    QtGui.QApplication.__init__(self, sys.argv)
    self.connect(self, SIGNAL("lastWindowClosed()"), self, SLOT("quit()"))
    self.win = uic.loadUi("myAppUI.ui")
    self.win.show()
    
if __name__ == "__main__":
  MyGUIApp().exec_()

The setup.py contents is:

#!/usr/bin/env python

from distutils.core import setup
import py2exe

setup(windows=[{"script": 'MyGUIApp.py'}],
      options={
          "py2exe": {
              "bundle_files": 1,
              "compressed": True,
              "dll_excludes": ["MSVCP90.dll"],
              "includes": ["sip"],
              }
          }  
     )

Open a command prompt in the folder and run:

python setup.py py2exe

The py2exe scripts are going to take over and quite verbosely indicate what they are doing. Once the scripts are done cd into the dist subfolder and run the .exe file (the GUI does nothing other than show it is running).

If you get an error message about invalid syntax in C:\Python27\Lib\site-packages\PyQt4\uic\port_v3 go to the folder where port_v3 is located and rename the __init__.py file to OBSOLETE__init__.py. The port_v3 is for Python3 scripts … and some are questioning its inclusion into Python2 packages.

If you are using matplotlib packages you will need to add matplotlib specific snippets to the setup.py file as seen here.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *