SaltyCrane Blog — Notes on JavaScript and web development

How to install PyQt 4.3 and Python 2.5 on Windows

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: ,

Comments


#1 Anonymous commented on :

Thank you for perfect instruction. THX


#2 Anonymous commented on :

The example line:

from Qt import *

should be:

from PyQt4.Qt import *

#3 Anonymous commented on :

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.


#4 sofeng commented on :

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.


#5 Tarjei commented on :

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

Also, this works with Qt 4.2.3 as well


#6 sofeng commented on :

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


#7 sofeng commented on :

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.


#8 Anonymous commented on :

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


#9 sofeng commented on :

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


#10 Anonymous commented on :

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.


#11 sofeng commented on :

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_()
    

#12 Nickname unavailable commented on :

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)


#13 Boom commented on :

What about SIP? Does this package have that covered?


#14 Boom commented on :

My Bad I still did it the very hard way... SIP dev tools are included!


#15 Marko commented on :

Thank you for this post. It saved a lot of time..


#16 Lemmi commented on :

That was easy. Thank you!


#17 Prashant commented on :

I have compiled a python(PyQt) script using cython(pure python mode) using gcc. importing the module is not showing any error but executing it showing an unhandled win32 exception. Here are the details: PyQt 4.6.1 Python 2.6.2 Win XP 32 Cython-0.11.2.win32-py2.6 qt-sdk-win-opensource-2009.03.1.exe

Not so good in c/c++ stuff. Any pointers will be greatly appreciated.

Thanks. Prashant


#18 Sarvotham commented on :

Hi,

I have python 2.6 installed. I downloaded PyQt-Py2.6-gpl-4.7.3-2 and installed that. When i execute the sample program I get the error message -

Traceback (most recent call last):
  File "C:/Python26/Code/sample_pyqt.pyw", line 4, in <module>
    from PyQt4.QtGui import *
ImportError: DLL load failed: The specified module could not be found.

The QtGui4 DLL is present in the PyQt4 bin. Don't know the reason for this error. Have any idea why this is happening? Any help would be highly appreciated.

Thanks.


#19 Adrian commented on :

I'm also getting the problem mentioned in comment #18, with Python 2.6.5 and PyQt 4.7.3-2 on Windows 7 64 bit. This problem was also brought up in the Riverbank PyQt mailing list (http://old.nabble.com/DLL-Problem-with-PyQt-Py3.1-gpl-4.7.3-1.exe-td28291670.html), but with no resolution.


#20 Tutku Dalmaz commented on :

I'm using Window7 64-bit and so I've downloaded and installed PYthpn 2.6.4 64-bit but when i installed pyqt26,i could not import from the interactive shell.

I solved this problem via following steps: 1)I removed python2.6.4 64 bit. 2)I installed python 2.6.4 32 bit

upto now i'm using pyqt and python properly :) thank you so much for your explanations.


#21 Anes commented on :

Hi , I am a new comer to PyQt world , just install and run your "Hello World" sample. Really a nice example and it work as a Charm . thanks my dear author for this good contribution.

Thankfully Anes P.A


#22 Gael commented on :

Hi, I'd love to use PyQt4 but any Windows install that i can find on the riverbank's website just fails on either XP 32 or Win7 64: "not a valid Win32 app". Any help would be appreciated!


#23 aaa commented on :

Same problem as #18 and #19.


#24 aaa commented on :

I had python 32 bits with Pyqt 64 bits + python path was incorrect in windows Path environnement variable. Replaced python 32 with 64, adding the good path... It works now :p

Thanks for the tutorials.