Packaging PySide applications on Windows

cx_Freeze

As reported on the mailing list by Srini Kommoori:
cx_Freeze area updated: August 21st, 2011 by Andre Mikulec

Here is the process to package your Python scripts.

1. Install cx_Freeze [cx-freeze.sourceforge.net]

2. Run:

  1. cxfreeze yourapp.py --target-dir dist --base-name Win32GUI --include-modules atexit,PySide.QtNetwork --icon yourapptaskgroup.ico

The result of the cxfreeze statement will create an ‘exe’ file and supporting files in dist directory.

“atexit” must be included in —include-modules, otherwise the generated exe will fail.

On Windows, note that the —icon yourapptaskgroup.ico parameter will insert the icon into the final executable: yourapp.exe,. This will be the icon that represents a task bar group that is a stack of applications (and not an individual application). To implement application icons, add statements in your Python PySide code that use the setWindowIcon method. (Yes, this ALSO must be done.)

Currently, I am not sure what the icon behaviors are on OS X and Linux desktops (but they are different than on Windows). If you need to freeze a PySide application on those platforms continue reading the following.

“Setting the Application Icon…”: http://doc.qt.nokia.com/4.7-snapshot/appicon.html

3. Copy and replace QtGui4.dll from C:\Python26\Lib\site-packages\PySide\ to the cxfreeze dist\.

The issue seems to be caused by cxfreeze pruning dlls. Once original dlls are copied in the distribution directory, everything works as expected. It seems that other DLLs may be causing trouble; just copy them from PySide’s directory.

4. To make .gif and .jpg files show in QWebView web pages, do the following:

In the ‘dist’ directory, create a folder named ‘imageformats.’ So the result looks like this.

  1. 'dist\imageformats'

Copy the files found in C:\Python26\Lib\site-packages\PySide\plugins\imageformats into the folder ‘dist\imageformats.’

4 ALT. Alternatively, instead of step 4, do all of the following:

4.1.1 In the ‘dist’ directory, create a folder named ‘plugins.’ So the result looks like this.

  1. 'dist\plugins'

4.1.2 Copy the folders and files found in C:\Python26\Lib\site-packages\PySide\plugins into the folder ‘dist\plugins’ (and mimic the ‘copy’ source directory structure within the ‘copy’ target)

4.1.3 In the dist directory, create a file named ‘qt.conf.’ So the result looks like this.

  1. 'dist\qt.conf'

4.1.4 In a text editor, open up qt.conf, type into the file the following information.

  1. [Paths]
  2. Binaries = .
  3. Plugins = plugins

4.2 Save the file. Close the text editor.

py2exe

Following the instructions in the py2exe tutorial [py2exe.org] works with PySide, with some notes:

  • If you use QtWebKit, QtNetwork must be included manually:
    1. ...
    2. setup(
    3.     ...
    4.     options = {
    5.         'py2exe': {
    6.             ...
    7.             'includes': ['PySide.QtNetwork'],
    8.             ...
    9.         }
    10.     }
    11. ...

  • If you use QtNetwork, the target machine must either have OpenSSL installed or the DLLs packaged in the same folder as PySide’s DLLs
  • If you use Phonon, the following helps:

  1. setup(
  2.     ...
  3.     data_files=[('phonon_backend',
  4.                 [r'C:\Python26\Lib\site-packages\PySide\plugins\phonon_'
  5.                  r'backend\phonon_ds94.dll'])],
  6.     ...
  7. )

pyinstaller

Follow the instructions in pyinstaller manual [pyinstaller.org]

In most cases, additional setup is not required, it will copy pyside and relevant dependencies in the dist folder.

Categories: