如何在M1系列的Mac上安装PyQt5?

最近公司要用到PyQt5,之前也在Windows弄过打包,单换个电脑后安装报错AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'后处理如下:

电脑基本情况

  • M1Pro芯片
  • Python3.9
  • venv/conda配置环境

配置

  • 安装brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • 安装pyqt@5
brew install pyqt@5
  • 找到用brew安装后的地方,一般在如下地方:注意版本的区别;
cd /opt/homebrew/Cellar/pyqt@5/5.15.7_1/lib/python3.9/site-packages
  • 把上面路径下的QT5复制到自己项目Python版本的依赖路径【我这里项目是pyqt5-test,环境是venv配置的】下:
sudo cp -r /opt/homebrew/Cellar/pyqt@5/5.15.7_1/lib/python3.9/site-packages/* /Users/emperinter/code/pyqt5-test/venv/lib/python3.9/site-packages/

测试

  • test code
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(600, 300)
        self.setWindowTitle('创建按钮和按钮点击事件的例子')
        self.button1 = QPushButton('按键1', self)
        self.button1.clicked.connect(self.clickButton)

    def clickButton(self):
        sender = self.sender()
        print(sender.text() + '被点击')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())
  • 效果如下


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *