Of course. Here is a comprehensive guide for using PyQt4 with Python 2.7.

Introduction: PyQt4 and Python 2.7
PyQt4 is a set of Python bindings for the Qt application framework. It was the standard for creating desktop applications with Python for many years, especially during the era of Python 2.7. While it is now deprecated and not recommended for new projects, it is essential for maintaining and running legacy applications.
Important Disclaimer: PyQt4 is no longer maintained. It does not support modern features, security patches, or Python 3. All new development should use PyQt5 or PySide2/PySide6 with Python 3.
Installation
Since PyQt4 is not available in the modern Python Package Index (pip), you need to install it from its original source repository. The standard way to do this is using easy_install, which was the predecessor to pip.
Prerequisites:

- Python 2.7 installed.
- A C++ compiler (like MinGW on Windows or GCC on Linux/macOS).
- The necessary Qt4 development libraries (headers and
.dll/.sofiles).
Installation Steps:
a) Install Dependencies (Linux/macOS)
On Debian/Ubuntu:
sudo apt-get update sudo apt-get install python2.7-dev python-qt4 qt4-dev-tools
On Fedora/CentOS:

sudo yum install python2-devel PyQt4 qt4-devel
On macOS (using Homebrew):
brew install python@2 qt
b) Install Dependencies (Windows)
This is the trickiest part. You need to download the Qt4 libraries for Windows.
- Download Qt4: Go to the Qt Archive and download the "Qt libraries 4.8.x for Windows (MinGW)" or "for VS2008/VS2010". Make sure to get the version that matches your compiler (e.g., if you have MinGW, get the MinGW version).
- Install: Run the installer and note the installation path (e.g.,
C:\Qt\4.8.6). - Add to System Path: Add the
bindirectory of your Qt installation to your system'sPATHenvironment variable. For example:C:\Qt\4.8.6\bin.
c) Install PyQt4 using easy_install
Open your command prompt or terminal and run:
easy_install PyQt4
easy_install will find the necessary header files from your Qt installation and compile the bindings from source.
A Simple "Hello World" Example
This is the classic way to create a window with PyQt4. Notice the import statements and the use of pyqtSignal and pyqtSlot.
# main.py
import sys
from PyQt4.QtCore import Qt, pyqtSignal, pyqtSlot
from PyQt4.QtGui import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout
# A simple custom widget
class MyWindow(QWidget):
# Define a custom signal
button_clicked = pyqtSignal(str)
def __init__(self):
super(MyWindow, self).__init__()
self.setWindowTitle("PyQt4 on Python 2.7")
self.setGeometry(100, 100, 300, 200)
# Create UI elements
self.label = QLabel("Click the button!")
self.label.setAlignment(Qt.AlignCenter)
self.button = QPushButton("Click Me")
# Connect the button's 'clicked' signal to our custom slot
self.button.clicked.connect(self.on_button_click)
# Connect our custom signal to the label's update method
self.button_clicked.connect(self.label.setText)
# Set up the layout
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.button)
self.setLayout(layout)
# Define a custom slot
@pyqtSlot()
def on_button_click(self):
print("Button was clicked!")
# Emit the custom signal with a message
self.button_clicked.emit("Hello from PyQt4!")
# Main application entry point
if __name__ == "__main__":
# Create a QApplication instance
app = QApplication(sys.argv)
# Create an instance of our custom window
window = MyWindow()
window.show()
# Start the event loop
sys.exit(app.exec_())
To run this code:
- Save it as
main.py. - Run it from your terminal:
python main.py
You should see a window with a label and a button. Clicking the button will update the label and print a message to the console.
Key Differences from Modern PyQt5/PySide2
If you're used to PyQt5, here are the key things to remember for PyQt4:
| Feature | PyQt4 | PyQt5 / PySide2 | Notes |
|---|---|---|---|
| Import | from PyQt4.QtGui import ... |
from PyQt5.QtWidgets import ... |
In PyQt5, QtWidgets contains most widget classes. |
| Signal/Slot | pyqtSignal, pyqtSlot |
pyqtSignal, pyqtSlot |
The syntax is the same, but they are imported from PyQt4.QtCore. |
| String Type | QString, QVariant |
Native Python str, int, dict |
This is a major difference. In PyQt4, you often had to convert between Python strings and QString objects. In PyQt5, strings are handled natively. |
| Module Structure | All in QtGui and QtCore |
Split into QtWidgets, QtCore, QtGui, QtMultimedia, etc. |
PyQt5 is more modular. |
| UI Tools | pyuic4 |
pyuic5 |
The command-line tool for compiling .ui files. |
| Designer | designer-4 |
designer |
The Qt Designer executable name. |
Migrating from PyQt4 to PyQt5 (A Quick Guide)
If you need to modernize an old PyQt4 project, here is a high-level migration strategy:
-
Change Imports:
from PyQt4.QtGui import QApplication, QWidget, QLabel->from PyQt5.QtWidgets import QApplication, QWidget, QLabelfrom PyQt4.QtCore import Qt, pyqtSignal->from PyQt5.QtCore import Qt, pyqtSignal
-
Handle String Types:
- Remove all
QStringusage. Replacemy_q_string.toUtf8()withmy_python_string.encode('utf-8')if needed for C APIs. - Remove
QVariantusage. Use native Python types directly.
- Remove all
-
Update Signal/Slot Connections:
- The old
SIGNAL/SLOTstring-based syntax still works in PyQt5 for compatibility, but the new style is preferred. - Old:
self.connect(self.button, SIGNAL('clicked()'), self.on_button_click) - New (PyQt4/5):
self.button.clicked.connect(self.on_button_click)(This is the same).
- The old
-
Fix
sys.exit():- In PyQt4,
app.exec_()was common. In PyQt5,app.exec()is the standard. - So,
sys.exit(app.exec_())becomessys.exit(app.exec()).
- In PyQt4,
-
Update UI Compilation:
- If you use Qt Designer, change your build script to use
pyuic5instead ofpyuic4.
- If you use Qt Designer, change your build script to use
Why is PyQt4 Obsolete?
- No Security Updates: It contains known vulnerabilities that will never be fixed.
- No Bug Fixes: Any bugs you encounter are permanent.
- No Python 3 Support: It's locked to the Python 2.7 era.
- No Modern Features: It lacks modern Qt features like High-DPI scaling, new widgets, and better integration with modern operating systems.
- No Community Support: The community has moved on to PyQt5 and PySide.
Conclusion: Use PyQt4 only when you absolutely must to maintain an existing legacy application. For any new project, choose a modern stack like PyQt5 or PySide6 with Python 3.
