该Qtwidgets
文件是目录中的编译.so
文件,PyQt5
所有模块也是如此,文件中没有导入,__init__.py
因此您需要使用 from ...
在空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
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
使用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
__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
因此,真正的区别以及您看到输出的原因是您尝试从第二个示例中的模块和第一个示例中的包导入。