Python PyQt on macOS Sierra

前端 未结 6 1321
忘了有多久
忘了有多久 2020-12-08 15:18

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.

相关标签:
6条回答
  • 2020-12-08 15:34

    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()
    
    0 讨论(0)
  • 2020-12-08 15:36

    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:

    1. Install Xcode (required by Qt5 installer)
    2. Install Python 3 from https://www.python.org/downloads/ (includes pip3 command)
    3. Install Qt5 from https://www.qt.io/
    4. Install SIP (pip3 install SIP)
    5. Install PyQt (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).

    0 讨论(0)
  • 2020-12-08 15:37

    Make sure you have homebrew installed.

    Use the following commands:

    1. brew tap cartr/qt4
    2. brew tap-pin cartr/qt4
    3. brew install qt
    4. brew install pyside
    0 讨论(0)
  • 2020-12-08 15:40

    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()
    
    0 讨论(0)
  • 2020-12-08 15:41

    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
    

    0 讨论(0)
  • 2020-12-08 15:48

    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.

    0 讨论(0)
提交回复
热议问题