Tuesday, December 29, 2009

PyQt and py2exe

PyQt is an excellent framework for creating GUI applications in Python. It would make sense to create an executable version of these applications that would not rely on Python being pre-installed. The py2exe tool makes this process very easy for most Python applications. However, I recently ran into the problem of an executable PyQt application not acting the same as its Python interpreted counterpart. For the record, I am using Python 2.6.4.

For example, PyQt4 comes with an example application called imageviewer.pyw. After creating an executable version of this application, any attempts to open a jpg file will fail. Opening a png file, however, will work.

The solution to this problem is described in more detail here. To summarize, PyQt uses a plugin to display jpg images. This plugin is not automatically included when using the py2exe tool. To manually include these plugins, do the following:
  1. In the py2exe created dist directory, create a subdirectory named PyQt4.
  2. Copy the plugins folder from the PyQt install location (typically C:\PythonXX\Lib\site-packages\PyQt4).
  3. Paste the plugins directory into the newly create PyQt4 subdirectory.
  4. From the PyQt4 install location, copy the qt.conf file.
  5. Paste the file into the py2exe dist directory.
  6. Edit the file so that the prefix is equal to ./PyQt4
  7. Test that the application works properly.

No comments:

Post a Comment