How to install PyQt 4.3 and Python 2.5 on Windows



This is my OLD blog. I've copied this post over to my NEW blog at:

http://www.saltycrane.com/blog/2007/01/how-to-install-pyqt-41-python-25-and-qt_8340/

You should be redirected in 2 seconds.



Update 1/10/2008: PyQt has made the install process a lot simpler because it has bundled everything you need in one installer including QT 4.3 open source edition. Now all you need to do is install Python and the PyQt bundle. Immediately following are the updated steps. Below that is the old instructions.

Update 7/1/2008: Updated for PyQt 4.4.2


NEW INSTRUCTIONS

Here are the steps to install and create a simple "Hello World" GUI application using PyQt 4.4, and Python 2.5, on Windows.


Install Python 2.5
  1. Go to http://www.python.org/download/ and click on "Python 2.5.x Windows installer"
  2. Save and run the Windows installer
  3. Go through the steps and accept the defaults.

Install the PyQt 4.4 bundle (including QT 4.4)
  1. Go to http://www.riverbankcomputing.co.uk/software/pyqt/download and select the "PyQt-Py2.5-gpl-4.4.2-1.exe" link.
  2. Save and run the file.
  3. Go through the steps and accept the defaults.

Run a "Hello World" application
  1. Go to "Start" -> "All Programs" -> "Python 2.5" -> "IDLE (Python GUI)"
  2. Open a new window ("File" -> "New window")
  3. Type the following code inside and save:
    import sys
    from PyQt4.QtGui import *
    app = QApplication(sys.argv)
    button = QPushButton("Hello World", None)
    button.show()
    app.exec_()
  4. Hit "F5" to run. A window with a single push button should pop up.

For more examples, go to "Start" -> "All Programs" -> "PyQt GPL v4.4.2 for Python v2.5" > "Examples" > "PyQt Examples Source" (For a default installation, this is also located at C:\Python25\PyQt4\examples.) To start, look in the "tutorial" directory.


OLD INSTRUCTIONS

Here are the steps to install and create a simple "Hello World" GUI application using PyQt 4.1.1, Python 2.5, and QT 4.2.2 Open Source edition (GPL) on Windows XP with the MinGW compiler.

Install Python 2.5
  1. Go to http://www.python.org/download/ and click on "Python 2.5 Windows installer"
  2. Save and run the Windows installer
  3. Go through the steps and accept the defaults.

Install MinGW
  1. Go to http://www.mingw.org/download.shtml
  2. Download the following "bin" files from the "Current" section:
    • gcc-core-3.4.2-20040916-1.tar.gz
    • gcc-g++-3.4.2-20040916-1.tar.gz
    • mingw-runtime-3.9.tar.gz
    • w32api-3.6.tar.gz
  3. Extract all the files to "c:\mingw"

Install QT 4.2.2 Open Source edition
  1. Go to the Open Source download page at http://www.trolltech.com/developer/downloads/qt/windows. Note there is also an Evaluation version. This is *not* the one you want.
  2. Under the "Download" heading, select the "http://ftp.iasi.roedu.net/mirrors/ftp.trolltech.com/qt/source/qt-win-opensource-4.2.2-mingw.exe" link.
  3. Go through the steps and accept the defaults.
  4. When you get to the MinGW page, leave the "Download and install minimal MinGW installation" box unchecked and make sure the location of the MinGW installation is set to "c:\mingw". Click "Install".
  5. You will get an error message which says that the installer could not find a valid "w32api.h" file. You can install the 3.2 version from the mingw site, but the 3.6 version works. Click "Yes" to continue. Click "Finish" to finish the installation.

Install PyQt 4.1.1
  1. Go to http://www.riverbankcomputing.co.uk/pyqt/download.php and select the "PyQt-gpl-4.1.1-Py2.5-Qt4.2.2.exe" link.
  2. Save and run the file.
  3. Go through the steps and accept the defaults.

Check your Environment Variables
  1. Right-click on "My Computer" and select "Properties"
  2. Click the "Advanced" tab
  3. Click "Environment Variables"
  4. The following variables should be set:
    • user variable QTDIR - "c:\qt\4.2.2"
    • user variable QMAKESPEC - "win32-g++"
    • system variable PATH - include "C:\Qt\4.2.2\bin;C:\Python25\Scripts;C:\Python25;C:\Python25\DLLs;"

Run a "Hello World" application
  1. Go to "Start" -> "All Programs" -> "Python 2.5" -> "IDLE (Python GUI)"
  2. Open a new window ("File" -> "New window")
  3. Type the following code inside and save:
    import sys
    from PyQt4.QtGui import *
    app = QApplication(sys.argv)
    button = QPushButton("Hello World", None)
    button.show()
    app.exec_()
  4. Hit "F5" to run.
Technorati tags: ,

12 comments:

Anonymous said...

Thank you for perfect instruction. THX

Anonymous said...

The example line:
from Qt import *
should be:
from PyQt4.Qt import *

Anonymous said...

I didn't need to manually install mingw.

The qt-win-opensource-4.2.2-mingw.exe 'Download and install minimal MingGW' option worked just fine.

This seems simpler to me.

Thank you for this post/guide.

sofeng said...

I'm glad the post was helpful. For me, "from PyQt4.Qt import *" didn't work. I'm not sure what the difference is. Also, it is great if the 'Download and install minimal MingGW' option works for you. I think maybe it didn't work for me because I am behind a proxy.

Tarjei said...

Thanks for these instructions, had some trouble getting the environment set up correctly.

Also, this works with Qt 4.2.3 as well

sofeng said...

Tarjei,
Glad it works for Qt 4.2.3 as well. What problems did you have setting up the environment?
-sofeng

sofeng said...

I updated the example code to use the new module import line:

from PyQt4.QtGui import *

instead of the old line:

from Qt import *

Sorry it took so long to fix this.

Anonymous said...

Looks like the installation process has become much easier because the pyqt package gives everything you need to start developing once python is installed.

http://www.riverbankcomputing.co.uk/pyqt/download.php

sofeng said...

Anonymous:
I noticed this also. I'll update the post. -sofeng

Anonymous said...

Hi, Your instructions were a life saver. Did you know your the only one on the net with simple instructions? Everywhere else its all "compile this, make install that, etc etc.. " I think its b/c windows is neglected...

Ok, now for my request. I ran the hello world app at the end of your instructions, but I don't know what to do from there. Can you add information like:

1) when running the hellow world app, a black python command prompt always comes up as a second window, I don't want that showing up in my apps, how do I get around that?

2) I noticed that eric4 is included in the riverbank pack. But where do I go to use it? all i see in the directory are some eric .bat files, but none open the IDE.

3) How do I package my applications up for distribution to end users? How do I define little icon that shows up in the top left corner of the app?

Thanks again for the awesome instructions.

sofeng said...

Anonymous:
Thanks, I'm glad the instructions were helpful. Here are my responses to your questions.

1. To run a python program without the Console window popping up, rename your .py file with a .pyw extension. You can also use pythonw.exe instead of python.exe to run your program.

2. I got an error message when trying to run the eric IDE also. Maybe you can try installing the latest version of Eric4.

3. Personally, I use py2exe to package my applications. It does a good job. I think there are other alternatives as well. To change the icon in your application window, see the documentation for QWidget's windowIcon property: http://doc.trolltech.com/4.3/qwidget.html#windowIcon-prop. For example, to add an icon to the hello world example:

import sys
from PyQt4.QtGui import *
app = QApplication(sys.argv)
button = QPushButton("Hello World", None)
icon = QIcon("c:/path/to/myicon.png")
button.setWindowIcon(icon)
button.show()
app.exec_()

f said...

Hi its me again from the previous post. After some research, I found why eric is broken: a couple bugs in the installer from riverbank.

1) it forces the docs to be installed in C:\Program Files\PyQt4\ even though the default installation directory is in C:\Python25\. I am unsure why it would do this, but it does. So all the tutorials are broken and none work from the start menu. I still can't get the tutorials to work even after copying them to the python25 directory.

2) The riverbank installer does not account for eric4 to load unless you install python in the default directory(C:\python25\), and then pyqt4 in the default directory(the python25 direactory}. Even if you are very careful, it still breaks. So you might want to add to your instructions how important it is to use the default directory. Once I uninstalled it all, and reinstalled it with the default directory, eric4 works by starting it from the start menu. I don't really know who to report this bug to. I emailed riverbank about it.

That is about all I have right now. Thanks again for the fast response and the tip to use .pyw. and the icon trick. I appreciate it.

Also, do you have any pointers of people using pyqt GPL on windows to do any cool utilities out there? I just can't seem to find many people out there doing it on windows (mostly they are on linux)

About

This is my *OLD* blog. I've copied all of my posts and comments over to my NEW blog at:

http://www.saltycrane.com/blog/.

Please go there for my updated posts. I will leave this blog up for a short time, but eventually plan to delete it. Thanks for reading.