Anu*_*dor 7 python import pyqt5 qtwidgets
我是新手,刚开始学习Python编程:
import sys
from PyQt5 import QtWidgets
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
mainWindow.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,每个都可以.但是当我运行下面的代码时,它失败并显示以下错误消息:app = PyQt5.QtWidgets.QApplication(sys.argv)AttributeError:'module'对象没有属性'QtWidgets'
import sys
import PyQt5
if __name__ == "__main__":
app = PyQt5.QtWidgets.QApplication(sys.argv)
mainWindow = PyQt5.Qtwidgets.QmainWindow()
mainWindow.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我的Python版本是2.7,我使用的是Qt5库,我的操作系统是OpenSUSE 13.2,当然是Linux的发行版.
该文件是目录中的Qtwidgets编译文件,所有模块也是如此,文件中没有导入,因此您需要使用 from ....soPyQt5__init__.py
在空test1.cpython-34m.so目录中 使用 cython 编译文件的示例表现出相同的行为:py3__init.__py
In [1]: import py3
In [2]: py3.test1
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-9aa45b2a49b6> in <module>()
----> 1 py3.test1
AttributeError: 'module' object has no attribute 'test1'
In [3]: from py3 import test1
In [4]: test1.foo()
Out[4]: 100
Run Code Online (Sandbox Code Playgroud)
PyQt5中的文件如下:
/usr/lib/python3/dist-packages/PyQt5$ ls
__init__.py
__pycache__
_QOpenGLFunctions_2_0.cpython-34m-x86_64-linux-gnu.so
QtCore.cpython-34m-x86_64-linux-gnu.so
Qt.cpython-34m-x86_64-linux-gnu.so
QtDBus.cpython-34m-x86_64-linux-gnu.so
QtDesigner.cpython-34m-x86_64-linux-gnu.so
QtGui.cpython-34m-x86_64-linux-gnu.so
QtHelp.cpython-34m-x86_64-linux-gnu.so
QtNetwork.cpython-34m-x86_64-linux-gnu.so
QtOpenGL.cpython-34m-x86_64-linux-gnu.so
QtPrintSupport.cpython-34m-x86_64-linux-gnu.so
QtTest.cpython-34m-x86_64-linux-gnu.so
QtWidgets.cpython-34m-x86_64-linux-gnu.so
uic
Run Code Online (Sandbox Code Playgroud)
使用cat您可以看到 中没有导入__init__.py:
$:/usr/lib/python3/dist-packages/PyQt5$ cat __init__.py
# Copyright (c) 2014 Riverbank Computing Limited <info@riverbankcomputing.com>
#
# This file is part of PyQt5.
#
# This file may be used under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation and appearing in
# the file LICENSE included in the packaging of this file. Please review the
# following information to ensure the GNU General Public License version 3.0
# requirements will be met: http://www.gnu.org/copyleft/gpl.html.
#
# If you do not wish to use this file under the terms of the GPL version 3.0
# then you may purchase a commercial license. For more information contact
# info@riverbankcomputing.com.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Run Code Online (Sandbox Code Playgroud)
__init__.py 因此,因为当您尝试使用时没有导入,PyQt5.Qtwidgets您会看到错误,因为该模块显然没有属性。
如果您添加了类似的内容,那么from . import QtWidgets当您从包__init__.py中导入模块时,您可以使用import PyQt5 PyQt5.Qtwidgets或PyQt5.Qtwidgets也可以使用空白 init 。Qtwidgets PyQt5
您可以看到,当import PyQt5您真正拥有一个模块时:
In [6]: import PyQt5
In [7]: type(PyQt5)
Out[7]: module
Run Code Online (Sandbox Code Playgroud)
因此,真正的区别以及为什么您看到所做的输出是您尝试从第二个示例中的模块和第一个示例中的包导入。
| 归档时间: |
|
| 查看次数: |
390 次 |
| 最近记录: |