How can I get to work PyQt 4 or 5 on a Mac with OS X Sierra? It seems that I have to wait for a new version of PyQt but I am not sure if that is actually true.
Considering PyQt4 is no longer actively supported by its creators, I'd recommend using PyQt5 (plus I found it much easier to get working). Once you've installed pip3
(you can use easy_install
) run the following commands in your terminal:
1) pip3 install sip
2) pip3 install PyQt5
You can then run the following sample app to see if everything is working:
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
button = QtWidgets.QPushButton("Hello, PyQt!")
window.setCentralWidget(button)
window.show()
app.exec_()
if __name__ == '__main__':
main()
I managed to get Qt5 with PyQt5 installed (on both 10.10.5 and 10.12) using these steps, which I learned from https://gist.github.com/guillaumevincent/10983814:
pip3
command)pip3 install SIP
)pip3 install PyQt5
)This also made commands such as pyuic5
available in Terminal.app (requires re-opening the Terminal window once to recognize the new search paths).
Make sure you have homebrew installed.
Use the following commands:
brew tap cartr/qt4
brew tap-pin cartr/qt4
brew install qt
brew install pyside
1:
brew install cartr/qt4/pyqt
brew link qt@4
2: go here and download https://riverbankcomputing.com/software/sip/download
and do
tar -xzvf sip-4.19.6.tar.gz
cd sip-4.19.6
python configure.py
make
make install
3: go here and download : https://riverbankcomputing.com/software/pyqt/download
and do
tar -xzvf PyQt4_gpl_mac-4.12.1.tar.gz
cd PyQt4_gpl_mac-4.12.1
python configure.py
make
make install
4: test in python:
import sys;
from PyQt4 import QtGui;
def pyqtDemo():
app = QtGui.QApplication(sys.argv);
w = QtGui.QWidget();
w.resize(250, 150);
w.move(300, 300);
w.setWindowTitle('Hello World');
w.show();
sys.exit(app.exec_());
pyqtDemo()
The easiest way to install PyQt (4 or 5) on OSX is probably using Homebrew. This will also install a separate standalone Python from the system Python, meaning it will continue to work without problems following any future system updates.
According to this thread PyQt4 is no longer supported on macOS Sierra, but PyQt5 will still work.
Once you've installed Homebrew, you can install PyQt5 with the following:
brew install pyqt5 # for PyQt5
If you still get the import error, you should also add
PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python2.7/site-packages/"
export PYTHONPATH
to your ~/.bash_profile
file after you applied the above stated steps, then it should work fine (make sure that PyQt4 is installed in that folder). I have installed python with conda and this import error seems to be related to anaconda.