--- author: '' category: '' date: 2011/07/24 21:20 description: '' link: '' priority: '' slug: BB955 tags: kde, programming, pyqt, pyqtbyexample, python, qt title: Shipping your PyQt app for windows type: text updated: 2011/07/24 21:20 url_type: '' --- I have written about this in the past, with the general conclusion being "it's a pain in the ass". So, now, here is how it's done. 1) Start with a working PyQt application. In this example, I will use `devicenzo.py `_ mostly because: a) It is a working PyQt application. b) It uses a big chunk of PyQt c) It's easy to test 2) Now you need a ``setup.py``. Here's one that works, with extensive commments. .. code-block:: python # We will be using py2exe to build the binaries. # You may use other tools, but I know this one. from distutils.core import setup import py2exe # Now you need to pass arguments to setup # windows is a list of scripts that have their own UI and # thus don't need to run in a console. setup(windows=['devicenzo.py'], options={ # And now, configure py2exe by passing more options; 'py2exe': { # This is magic: if you don't add these, your .exe may # or may not work on older/newer versions of windows. "dll_excludes": [ "MSVCP90.dll", "MSWSOCK.dll", "mswsock.dll", "powrprof.dll", ], # Py2exe will not figure out that you need these on its own. # You may need one, the other, or both. 'includes': [ 'sip', 'PyQt4.QtNetwork', ], # Optional: make one big exe with everything in it, or # a folder with many things in it. Your choice # 'bundle_files': 1, } }, # Qt's dynamically loaded plugins and py2exe really don't # get along. data_files = [ ('phonon_backend', [ 'C:\Python27\Lib\site-packages\PyQt4\plugins\phonon_backend\phonon_ds94.dll' ]), ('imageplugins', [ 'c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qgif4.dll', 'c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qjpeg4.dll', 'c:\Python27\lib\site-packages\PyQt4\plugins\imageformats\qsvg4.dll', ]), ], # If you choose the bundle above, you may want to use this, too. # zipfile=None, ) 3) Run ``python setup.py py2exe`` and get a ``dist`` folder full of binary goodness. And that's it. Except of course, that's *not* it. What this will do is create a binary set, either a folder full of things, or a single EXE file. And that's not enough. You have to consider at least the following: a) Put everything in resource files: images, qss files, icons, etc. Every file your app needs? Put it in a resource file and load it from there. That way you don't have to care about them if you go the "one exe" road. b) Compile .ui files to .py (same reason) c) Figure out if you use Qt's plugins, and make them work. This includes: using Phonon, using QtSQL, and using any image formats other than PNG. After you have that, are you done? NO! Your windows user will want an installer. I am not going to go into details, but I had a good time using BitRock's InstallBuilder for Qt. It's a nice tool, and it works. That's a lot in this field. But is that all? NO! You have to take care of the Visual Studio Runtime. My suggestion? Get a copy of the 1.1MB vcredist_x86.exe (not the larger one, the 1.1MB one), and either tell people to install it manually, or add it to your installer. You are legally allowed (AFAIK) to redistribute that thing as a whole. But not what's in it (unless you have a VS license). And we are done? NO! Once you run your app "installed", if it ever prints anything to stderr, you will get either a dialog telling you it did, or worse (if you are in aything newer than XP), a dialog telling you it can't write to a log file, and the app will never work again. This is because py2exe catches stderr and tries to save it on a logfile. Which it tries to create in the same folder as the binary. Which is usually not allowed because of permissions. Solution? Your app should never write to stderr. Write an excepthook and catch that. And then remove stderr or replace it with a log file, or something. Just don't let py2exe do it, because the way py2exe does it is broken. And is that it? Well, basically yes. Of course you should get 4 or 5 different versions of windows to test it on, but you are pretty much free to ship your app as you wish. Oh, mind you, don't upload it to downloads.com because they will wrap your installer in a larger one that installs bloatware and crap. So, there you go.