Python 导入错误:没有名为 PyQt4 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16994232/
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
ImportError: No module named PyQt4
提问by Win83
I installed pyqt4 by using Homebrew. But when I import PyQt4 in python interpreter, It said that "No module named PyQt4". Can somebody help me with that?
我使用 Homebrew 安装了 pyqt4。但是当我在 python 解释器中导入 PyQt4 时,它说“没有名为 PyQt4 的模块”。有人可以帮我吗?
回答by Anthony Kong
It is likely that you are running the python executable from /usr/bin (Apple version) instead of /usr/loca/bin (Brew version)
您可能正在从 /usr/bin(Apple 版本)而不是 /usr/loca/bin(Brew 版本)运行 python 可执行文件
You can either
你可以
a) check your PATH variable
a) 检查你的 PATH 变量
or
或者
b) run brew doctor
b) 运行 brew doctor
or
或者
c) run which python
c) 运行 which python
to check if it is the case.
检查是否是这种情况。
回答by Samuel John
After brew install pyqt, you can brew test pyqtwhich will use the python you have got in your PATH in oder to do the test (show a Qt window).
之后brew install pyqt,您可以brew test pyqt使用您在 PATH 中获得的 python 进行测试(显示 Qt 窗口)。
For non-brewed Python, you'll have to set your PYTHONPATH as brew info pyqtwill tell.
对于非酿造的 Python,您必须按照说明设置 PYTHONPATH brew info pyqt。
Sometimes it is necessary to open a new shell or tap in order to use the freshly brewed binaries.
有时需要打开一个新的外壳或水龙头才能使用新酿造的二进制文件。
I frequently check these issues by printing the sys.path from inside of python:
python -c "import sys; print(sys.path)"The $(brew --prefix)/lib/pythonX.Y/site-packageshave to be in the sys.pathin order to be able to import stuff. As said, for brewed python, this is default but for any other python, you will have to set the PYTHONPATH.
我经常打印从蟒蛇里面的sys.path中检查这些问题:
python -c "import sys; print(sys.path)"在$(brew --prefix)/lib/pythonX.Y/site-packages必须的sys.path,以便能够进口的东西。如前所述,对于 brewed python,这是默认设置,但对于任何其他 python,您必须设置PYTHONPATH.
回答by Lily
You have to check which Python you are using. I had the same problem because the Python I was using was not the same one that brew was using. In your command line:
您必须检查您使用的是哪种 Python。我遇到了同样的问题,因为我使用的 Python 与 brew 使用的 Python 不同。在您的命令行中:
which python
output: /usr/bin/pythonwhich brew
output: /usr/local/bin/brew //so they are differentcd /usr/local/lib/python2.7/site-packagesls//you can see PyQt4 and sip are here- Now you need to add
usr/local/lib/python2.7/site-packagesto your python path. open ~/.bash_profile//you will open your bash_profile file in your editor- Add
'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH'to your bash file and save it - Close your terminal and restart it to reload the shell
pythonimport PyQt4// it is ok now
which python
输出:/usr/bin/pythonwhich brew
输出:/usr/local/bin/brew //所以它们是不同的cd /usr/local/lib/python2.7/site-packagesls//你可以看到PyQt4和sip在这里- 现在您需要添加
usr/local/lib/python2.7/site-packages到您的python 路径。 open ~/.bash_profile//您将在编辑器中打开 bash_profile 文件- 添加
'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH'到您的 bash 文件并保存 - 关闭终端并重新启动它以重新加载外壳
pythonimport PyQt4// 现在好了
回答by Vasin Yuriy
I solved the same problem for my own program by installing python3-pyqt4.
我通过安装python3-pyqt4.
I'm not using Python 3 but it still helped.
我没有使用 Python 3,但它仍然有帮助。
回答by Jacob Beauchamp
If you're using Anaconda to manage Python on your system, you can install it with:
如果您使用 Anaconda 来管理系统上的 Python,您可以使用以下命令安装它:
$ conda install pyqt=4
$ conda install pyqt=4
Omit the =4to install the most current version.
省略=4安装最新版本。
Answer from How to install PyQt4 in anaconda?

