Mac 安装 pyqt

最近在研究 python 数据可视化(画图),网上看到一段代码:

from PyQt4 import QtGui, QtCore

电脑上没有安装 PyQt4,肯定是报错的,所以要安装 PyQt,后面进行 Qt 和 Python 的交叉编译可能也会用到。

1. PyQt

1.1. What is PyQt

PyQt is a set of Python v2 and v3 bindings for The Qt Company's Qt application framework and runs on all platforms supported by Qt including Windows, OS X, Linux, iOS and Android. PyQt5 supports Qt v5. PyQt4 supports Qt v4 and will build against Qt v5. The bindings are implemented as a set of Python modules and contain over 1,000 classes.

PyQt4 and Qt v4 are no longer supported and no new releases will be made. PyQt5 and Qt v5 are strongly recommended for all new development.

PyQt is dual licensed on all supported platforms under the GNU GPL v3 and the Riverbank Commercial License. Unlike Qt, PyQt is not available under the LGPL. You can purchase the commercial version of PyQt here. More information about licensing can be found in the License FAQ.

PyQt does not include a copy of Qt. You must obtain a correctly licensed copy of Qt yourself. However, binary wheels of the GPL version of PyQt5 are provided and these include a copy of the LGPL version of Qt.

1.2. Why PyQt

PyQt brings together the Qt C++ cross-platform application framework and the cross-platform interpreted language Python.

Qt is more than a GUI toolkit. It includes abstractions of network sockets, threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, XML, a fully functional web browser, a help system, a multimedia framework, as well as a rich collection of GUI widgets.

Qt classes employ a signal/slot mechanism for communicating between objects that is type safe but loosely coupled making it easy to create re-usable software components.

Qt also includes Qt Designer, a graphical user interface designer. PyQt is able to generate Python code from Qt Designer. It is also possible to add new GUI controls written in Python to Qt Designer.

Python is a simple but powerful object-orientated language. Its simplicity makes it easy to learn, but its power means that large and complex applications can be created. Its interpreted nature means that Python programmers are very productive because there is no edit/compile/link/run development cycle.

Much of Python's power comes from its comprehensive set of extension modules providing a wide variety of functions including HTTP servers, XML parsers, database access, data compression tools and, of course, graphical user interfaces. Extension modules are usually implemented in either Python, C or C++. Using tools such as SIP it is relatively straight forward to create an extension module that encapsulates an existing C or C++ library. Used in this way, Python can then become the glue to create new applications from established libraries.

PyQt combines all the advantages of Qt and Python. A programmer has all the power of Qt, but is able to exploit it with the simplicity of Python.

For more details, including some simple programming examples, you can download a copy of the PyQt Whitepaper.

2. 系统环境

硬件:Mac mini
软件:macOS Mojave 10.14

3. 开始安装

建议使用 brew 安装,网上说使用源码安装会有好多坑,有坑就别去踩吧。如果还没有安装brew,请自行百度。

3.1. 正常安装

打开终端,输入如下命令:

brew install pyqt

提示 Updating Homebrew...,请耐心等待,可能需要十几分钟:


updating.png

开始下载 brew 更新包、自动更新了 Homebrew、新的 Formulae(只截取了一部分):


homebrew.png

Updated Formulae:
formulae.png

Renamed Formulae、Deleted Formulae:


Deleled Formulae.png

开始安装所需依赖:
dependedies.1png

dependedies2.png

3.2. 更新 Xcode

安装 python 时报错:


python error.png

Mac里预装了python2.7.10,我又同时装了python3.7.0,不知道这里为啥还要去装python,先按照提示更新Xcode,打开App Store,更新:


update xcode.png

更新比较缓慢(视网络情况),请耐心等待:
waiting.png

3.3. 重试

执行命令:

brew install pyqt

报错:


error.png

更换安装方法,见下节。

3.4. 使用 pip3 安装

pip3 install pyqt5
pip3_install_pyqt5.png

4. 测试

在终端可以导入 PyQt5 :


image.png

在Idea中也可以导入:


5

你可能感兴趣的:(Mac 安装 pyqt)