Il file Qtwidgets
è un .so
file compilato nella directory PyQt5
, come lo sono tutti i moduli, non esistono importazioni nel file __init__.py
quindi è necessario utilizzare da ...
Un esempio utilizzando un Cython compilato presentare test1.cpython-34m.so
in una directory py3
con un vuoto __init.__py
presenta lo stesso comportamento:
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
I file in PyQt5 sono le seguenti:
/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
Usando cat
si può vedere che non esistono importazioni in __init__.py
:
$:/usr/lib/python3/dist-packages/PyQt5$ cat __init__.py
# Copyright (c) 2014 Riverbank Computing Limited <[email protected]>
#
# 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
# [email protected]
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Quindi perché non ci sono le importazioni nella __init__.py
quando si tenta di utilizzare PyQt5.Qtwidgets
si ottiene l'errore che si vede come il modulo, ovviamente, non ha attributi
Se si è aggiunto qualcosa di simile from . import QtWidgets
al __init__.py
si potrebbe quindi utilizzare import PyQt5 PyQt5.Qtwidgets
o PyQt5.Qtwidgets
sarebbe anche lavorare con un init vuota come si sta importando il modulo Qtwidgets
dal pacchettoPyQt5
.
Si può vedere quando si import PyQt5
voi allora in realtà dispone di un modulo:
In [6]: import PyQt5
In [7]: type(PyQt5)
Out[7]: module
Quindi la vera differenza e perché si vede l'uscita si fa è che si sta tentando di importare da un modulonel secondo esempio e un pacchetto nel primo.
'QtWidgets' è una classe definita in' __init __ py' di 'PyQt5'.. Vedi http://stackoverflow.com/questions/582723/how-to-import-classes-defined-in-init-py – Selcuk
Ho appena letto il post che hai inviato. Ma ancora confuso, ti piacerebbe spiegare chiaramente? Grazie. @Selcuk – Anudorannador