ファイルはディレクトリ内の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 を使用するか、または__init__.py
使用することができます。import PyQt5 PyQt5.Qtwidgets
PyQt5.Qtwidgets
Qtwidgets
PyQt5
import PyQt5
実際にモジュールを作成すると、次のようになります。
In [6]: import PyQt5
In [7]: type(PyQt5)
Out[7]: module
したがって、実際の違いと、出力が表示される理由は、2 番目の例ではモジュールから、最初の例ではパッケージからインポートしようとしていることです。