macOS Sierra 上的 Python PyQt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39821177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python PyQt on macOS Sierra
提问by Bzzzt_90
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.
如何在装有 OS X Sierra 的 Mac 上使用 PyQt 4 或 5?似乎我必须等待 PyQt 的新版本,但我不确定这是否真的如此。
回答by Fardin Allahverdi
回答by Kal
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:
考虑到 PyQt4 的创建者不再积极支持,我建议使用 PyQt5(而且我发现它更容易工作)。安装pip3
(可以使用easy_install
)后,在终端中运行以下命令:
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()
回答by mfitzp
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.
在 OSX 上安装 PyQt(4 或 5)的最简单方法可能是使用Homebrew。这还将从系统 Python 中安装一个单独的独立 Python,这意味着它可以在未来的任何系统更新后继续工作而不会出现问题。
According to this threadPyQt4 is no longer supported on macOS Sierra, but PyQt5 will still work.
根据这个线程,macOS Sierra 不再支持 PyQt4,但 PyQt5 仍然可以工作。
Once you've installed Homebrew, you can install PyQt5 with the following:
安装 Homebrew 后,您可以使用以下命令安装 PyQt5:
brew install pyqt5 # for PyQt5
回答by Thomas Tempelmann
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:
我设法使用这些步骤安装了带有 PyQt5 的 Qt5(在 10.10.5 和 10.12 上),这是我从https://gist.github.com/guillaumevincent/10983814学到的:
- Install Xcode(required by Qt5 installer)
- Install Python 3 from https://www.python.org/downloads/(includes
pip3
command) - Install Qt5 from https://www.qt.io/
- Install SIP(
pip3 install SIP
) - Install PyQt(
pip3 install PyQt5
)
- 安装Xcode(Qt5 安装程序需要)
- 从https://www.python.org/downloads/安装 Python 3 (包括
pip3
命令) - 从https://www.qt.io/安装 Qt5
- 安装SIP(
pip3 install SIP
) - 安装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).
这也使得诸如pyuic5
Terminal.app 中的命令可用(需要重新打开终端窗口一次以识别新的搜索路径)。
回答by Kristina
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.
~/.bash_profile
应用上述步骤后到您的文件,那么它应该可以正常工作(确保 PyQt4 安装在该文件夹中)。我已经用 conda 安装了 python,这个导入错误似乎与 anaconda 有关。
回答by John Q
1:
1:
brew install cartr/qt4/pyqt
brew link qt@4
2: go here and download https://riverbankcomputing.com/software/sip/download
2:去这里下载 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
3:到这里下载: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:
4:在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()