RkBlog

Hardware, programming and astronomy tutorials and reviews.

Building standalone MS Windows apps with py2exe

How to make standalone Windows applications from Python and PyQt4 scripts using py2exe.

Building standalone application from Python/PyQt4 scripts under Windows requires py2exe package. When you have it you can build the app using a config file, usualy setup.py. Here is an example config:
from distutils.core import setup
import py2exe

setup(windows=[{"script" : "MYFILE.py"}], options={"py2exe" : {"includes" : ["sip", ]}})
To build the app run:
python setup.py py2exe
Which will build the application. Sometimes it may stop reporting missing DLLs. Download them from the net and place them in the script folder. If you use Python 2.6 you may need Microsoft Visual C++ 2008 Redistributable Package.
If you try tu run the standalone application and Windows says that the configuration is invalid then you have to install Microsoft Visual C++ 2008 Redistributable Package (note that to app users)

Problem with missing PyQt4 components - QtWebkit

In some cases py2exe wont add all needed libraries. If you use for example QtWebkit, then you will have to manualy add QtNetwork (as its needed by QtWebkit, and not added by py2exe).
from distutils.core import setup
import py2exe

setup(windows=[{"script" : "MYFILE.py"}], options={"py2exe" : {"includes" : ["sip", "PyQt4.QtNetwork"]}})

Images, icons for my GUI

You should use a Resource file (qrc), and it will be build in into the binary package (as pyrcc4 converts it to a Python file). You don't have add source images to the package.

Standalone application doesn't supports images

If you app can display some images and you see nothing (or blue "?" icons), then imageformats DLLs didn't get included. There are two solutions. First is to make a "imageformats" folder in the folder with standalone application, and copy to it all needed DLLs from PyQt4, for example C:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\. The second one - if you use resource file for GUI icons or other things try adding simple (1x1 px even) image of extensions you want to support (If you have only PNGs in resource file, then JPG support may not be available in standalone version. If you add a JPG file to resources - then jpg support should also be added to standalone version).
RkBlog

PyQt and GUI, 10 June 2009,

Comment article