我在Python 3.4中编写了一个程序,我想为它制作一个GUI。我发现PyQt5是它的工具。
1)我已经下载并安装了PyQt5的二进制包(http://sourceforge.net/projects/pyqt/files/PyQt5/PyQt-5.4.1/PyQt5-5.4.1-gpl-Py3.4-Qt5.4.1-x32.exe)。
2)我试图在Python中运行这个示例代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
Python返回错误:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PyQt5.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget
File "D:\PyQt5.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget
ImportError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package
3)我找到了安装QT的建议。所以,我下载并安装了QT(http://download.qt.io/official_releases/online_installers/qt-opensource-windows-x86-online.exe)。
4)然后我卸载并重新安装了PyQt5的二进制包。 没有结果。
如果我运行,Python不会返回任何错误:
import PyQt5
但如果我试图跑:
from PyQt5.QtWidgets import QApplication, QWidget
它返回与开头相同的错误。
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PyQt5.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget
File "D:\PyQt5.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget
ImportError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package
我做错了什么?
答案 0 :(得分:2)
作为Python documentation states,包含输入脚本的目录位于所有其他搜索路径之前,除非sys.path
列表被篡改。
您的目录D:\
已包含正在导入的模块PyQt5
。尝试从不是包的模块导入子模块QtWidgets
,然后导致以下错误:
File "D:\PyQt5.py", line 2, in <module> from PyQt5.QtWidgets import QApplication, QWidget ImportError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package
因此,不要将程序文件称为包名称。
答案 1 :(得分:0)
对于那里的googlers来说,这就解决了我的问题:
您需要确保导入sys并为导入命令设置正确的大小。
import sys
from PyQt5 import QtGui
这是PyQt5
字符串的正确大小写。我花了一段时间才弄明白。