7

私は初心者で、Python プログラミングを学び始めたばかりです。

import sys
from PyQt5 import QtWidgets

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    mainWindow = QtWidgets.QMainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

上記のコードを実行すると、すべて問題ありません。しかし、以下のコードを実行すると、次のエラー メッセージで失敗します: app = PyQt5.QtWidgets.QApplication(sys.argv) AttributeError: 'module' object has no attribute 'QtWidgets'

import sys
import PyQt5
if __name__ == "__main__":
    app = PyQt5.QtWidgets.QApplication(sys.argv)

    mainWindow = PyQt5.Qtwidgets.QmainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

ちなみに、私の Python バージョンは 2.7 で、Qt5 ライブラリを使用しており、オペレーティング システムはもちろん Linux のディストリビューションである OpenSUSE 13.2 です。

4

2 に答える 2

1

ファイルはディレクトリ内の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.QtwidgetsPyQt5.QtwidgetsQtwidgets PyQt5

import PyQt5実際にモジュールを作成すると、次のようになります。

In [6]: import PyQt5

In [7]: type(PyQt5)
Out[7]: module

したがって、実際の違いと、出力が表示される理由は、2 番目の例ではモジュールから、最初の例ではパッケージからインポートしようとしていることです。

于 2015-03-17T21:06:12.667 に答える