Deploying PyQt applications on Windows

July 12th, 2007 by exhuma.twn

My preferred way to deploy python applications on Windows is to use py2exe.

py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.

If you run in trouble with sip, read on…

Sure there are other options to create windows executables. The ones I know of:

py2exe

The reason why I prefer py2exe, and why I would recommend using it, is because it’s right now the most widely used tool. So you will find lots of hits when googling for it. This is a big bonus. Additionally, it’s configuration is very flexible, and works well with dependancies (let’s say you are using SQLAlchemy, or some other third-party python-module).

The others either did not offer enough flexibility or had trouble detecting imported modules. With Freeze I was not even able to get the program to compile following their example scripts.

py2exe with PyQt

So you have a Qt program written in python and it works on your machine? Great! To get this to work you actually need to have the Qt libraries and the Python interpreter installed. But you don’t necessarily want your end-users to install these. To prevent that, you can use py2exe which will give you a directory with all the required libraries, byte-compiled sources and a ready-to-use windows-executable.

In order to create an executable, you write a standard python distutils setup script:

from distutils.core import setup
import py2exe
setup(
   windows = [{"script": "MyApplication.py"}]
   )

Then you can create the executable running the setup script with “py2exe” as build target:

$ python setup.py py2exe

With Qt you will run into trouble however. You will most certainly get an error along the lines:

  File "mainwindow.pyc", line 11, in ?
  File "qt.pyc", line 9, in ?
  File "qt.pyc", line 7, in __load
ImportError: No module named sip

The reason here is that the sip module is required by PyQt, and it’s not found in a place where py2exe could see that. So you need to tell py2exe to include sip even though it does not seem to be imported anywhere:

from distutils.core import setup
import py2exe
setup(
   windows = [{"script": "MyApplication.py"}],
   options = {"py2exe":{"includes":["sip"]}}
   )

This will take care of this problem, an your application should run just fine on windows.

Note

py2exe will give you a list of additional dll’s that may be needed. So far I could live without these dll’s. If you want to be on the safe side, you could include those dll’s into your project. But with the python-interpreter and the Qt libraries, your project will already be bloated enough (usually around 20MB). So there’s no need to waste even more space. But, as always, your mileage may vary!

Posted in Python | 1 Comment »

Pages

Recent Posts

Categories

Links


Archives

Meta