Python 导入错误:没有名为 PytQt5 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20672918/
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 PytQt5
提问by Thu Ra
following are my python, qt and sip versions
以下是我的 python、qt 和 sip 版本
root@thura:~# python -V
Python 2.7.3
root@thura:~# qmake --version
QMake version 3.0
Using Qt version 5.0.2 in /usr/lib/i386-linux-gnu
root@thura:~# sip -V
4.15.3
I tried to import the PyQt5 by following by this
我试图通过以下操作来导入PyQt5这
from PyQt5.QtWidgets import QtGui, QtCore
I got the following error
我收到以下错误
ImportError: No module named PyQt5.QtWidgets
导入错误:没有名为 PyQt5.QtWidgets 的模块
How can I solve this error.
我该如何解决这个错误。
Updated =====================
更新 ======================
When I tried to PyQt4, I got following error.
当我尝试 PyQt4 时,出现以下错误。
from PyQt4.QtCore import pyqtSlot as Slot RuntimeError: the sip module implements API v10.0 to v10.1 but the PyQt4.QtCore module requires API v8.1
from PyQt4.QtCore import pyqtSlot as Slot RuntimeError: the sip module implementations API v10.0 to v10.1 but the PyQt4.QtCore module requires API v8.1
Updated 2013-12-20 ======================================
2013 年 12 月 20 日更新 ======================================
1) download sip-4.15.3.tar.gz from here
2) extract sip-4.15.3.tar.gz
3) copy sip-4.15.3 to /home/thura
4) type "cd /home/thura/sip-4.15.3"
5) type "python configure.py", press enter, follow the instructions (type yes and press enter)
6) type "make", press enter and type "make install", press enter
7) download PyQt-gpl-5.1.1.tar.gz from here
8) extract PyQt-gpl-5.1.1.tar.gz
9) copy PyQt-gpl-5.1.1 folder to /home/thura folder.
10) type "cd /home/thura/PyQt-gpl-5.1.1"
11) type "python configure.py", press enter, following the instructions (type yes and press enter)
12)type "make", press enter and type "make install", press enter
1) 从这里下载 sip-4.15.3.tar.gz
2) 解压 sip-4.15.3.tar.gz
3) 将 sip-4.15.3 复制到 /home/thura
4) 输入“cd /home/thura/sip-4.15.3”
5) 输入“python configure.py”,回车,按照说明操作(输入yes,然后回车)
6)输入“make”,回车,输入“make install”,回车
7) 从这里下载 PyQt-gpl-5.1.1.tar.gz
8) 提取 PyQt-gpl-5.1.1.tar.gz
9) 将 PyQt-gpl-5.1.1 文件夹复制到 /home/thura 文件夹。
10) 输入“cd /home/thura/PyQt-gpl-5.1.1”
11)输入“python configure.py”,按回车,按照说明操作(输入yes然后回车)
12)输入“make”,回车,输入“make install”,回车
update 2013-12-20 =====================
更新 2013-12-20 ======================
After redo it again. I got the following error
重做之后再做。我收到以下错误
make[2]: Entering directory `/home/thura/PyQt/qpy/QtDBus'
make[2]: Nothing to be done for `install'.
make[2]: Leaving directory `/home/thura/PyQt/qpy/QtDBus'
make[1]: Leaving directory `/home/thura/PyQt/qpy'
cd QtCore/ && ( test -e Makefile || /usr/lib/i386-linux-gnu/qt5/bin/qmake /home/thura/PyQt/QtCore/QtCore.pro -o Makefile ) && make -f Makefile install
make[1]: Entering directory `/home/thura/PyQt/QtCore'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DSIP_PROTECTED_IS_PUBLIC -Dprotected=public -DQT_NO_DEBUG -DQT_PLUGIN -DQT_CORE_LIB -I/usr/share/qt5/mkspecs/linux-g++ -I. -I/usr/local/include/python2.7 -I../qpy/QtCore -I/usr/include/qt5 -I/usr/include/qt5/QtCore -I. -o sipQtCoreQtWindowStates.o sipQtCoreQtWindowStates.cpp
In file included from sipQtCoreQtWindowStates.cpp:24:0:
sipAPIQtCore.h:28:17: fatal error: sip.h: No such file or directory
compilation terminated.
make[1]: *** [sipQtCoreQtWindowStates.o] Error 1
make[1]: Leaving directory `/home/thura/PyQt/QtCore'
make: *** [sub-QtCore-install_subtargets-ordered] Error 2
回答by Dan
This probably means that python doesn't know where PyQt5 is located. To check, go into the interactive terminal and type:
这可能意味着 python 不知道 PyQt5 所在的位置。要检查,请进入交互式终端并键入:
import sys
print sys.path
What you probably need to do is add the directory that contains the PyQt5 module to your PYTHONPATHenvironment variable. If you use bash, here's how:
您可能需要做的是将包含 PyQt5 模块的目录添加到您的PYTHONPATH环境变量中。如果您使用bash,方法如下:
Type the following into your shell, and add it to the end of the file ~/.bashrc
在 shell 中键入以下内容,并将其添加到文件末尾 ~/.bashrc
export PYTHONPATH=/path/to/PyQt5/directory:$PYTHONPATH
where /path/to/PyQt5/directoryis the path to the folder where the PyQt5 library is located.
这里/path/to/PyQt5/directory是通往哪里的PyQt5库所在的文件夹。
回答by Thu Ra
After getting the help from @Blender, @ekhumoro and @Dan, I understand the Linux and Python more than before. Thank you. I got the an idea by @ekhumoro, it is I didn't install PyQt5 correctly. So I delete PyQt5 folder and download again. And redo everything from very start.
在得到@Blender、@ekhumoro 和@Dan 的帮助后,我比以前更了解 Linux 和 Python。谢谢你。我从@ekhumoro 得到了一个想法,是我没有正确安装 PyQt5。所以我删除了 PyQt5 文件夹并重新下载。并从一开始就重做一切。
After redoing, I got the error as my last update at my question. So, when I search at stack, I got the following solution from here
重做后,我收到了错误,这是我对问题的最后一次更新。所以,当我在堆栈中搜索时,我从这里得到了以下解决方案
sudo ln -s /usr/include/python2.7 /usr/local/include/python2.7
And then, I did "sudo make" and "sudo make install" step by step. After "sudo make install", I got the following error. But I ignored it and I created a simple design with qt designer. And I converted it into python file by pyuic5. Everything are going well.
然后,我一步一步地做了“sudo make”和“sudo make install”。“sudo make install”后,出现以下错误。但我忽略了它,我用 qt 设计器创建了一个简单的设计。我通过pyuic5将其转换为python文件。一切进展顺利。
install -m 755 -p /home/thura/PyQt/pyuic5 /usr/bin/
strip /usr/bin/pyuic5
strip:/usr/bin/pyuic5: File format not recognized
make: [install_pyuic5] Error 1 (ignored)
回答by artificerpi
If you are on ubuntu, just install pyqt5 with apt-getcommand:
如果您使用的是 ubuntu,只需使用以下apt-get命令安装 pyqt5 :
sudo apt-get install python3-pyqt5 # for python3
or
或者
sudo apt-get install python-pyqt5 # for python2
However, on Ubuntu 14.04 the python-pyqt5 package is left out [source] and need to be installed manually [source]
但是,在 Ubuntu 14.04 上,python-pyqt5 包被排除在 [ source] 之外,需要手动安装 [ source]
回答by tom
this can be solved under MacOS X by installing pyqt with brew
这可以在 MacOS X 下通过使用 brew 安装 pyqt 来解决
brew install pyqt
回答by Durgesh Kumar
pip install pyqt5for python3 for ubuntu
pip install pyqt5用于 ubuntu 的 python3

