正如标题所说,我对子包装感到困惑。我的包结构如下:
draw \
__init__.py
base \
__init__.py
utils.py
events.py
master.py
basegui.py
现在,draw.base.events
的第一行如下:
import draw.base.utils as _utils
draw.base
的第一行是:
from draw.base.events import Event, RenderEvent, InputEvent, MouseEvent, KeyboardEvent
只需使用IDLE检查SyntaxErrors的代码:
import draw.base as base
提供以下AttributeError
:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import draw.base
File "Z:\Eigene Dateien\Eigene Dokumente\Python\draw\base\__init__.py", line 4, in <module>
import draw.base.events as events
File "Z:\Eigene Dateien\Eigene Dokumente\Python\draw\base\events.py", line 10, in <module>
import draw.base.utils as _utils
AttributeError: 'module' object has no attribute 'base'
有人可以向我解释这个问题吗?
答案 0 :(得分:2)
要在draw.base.utils
中导入draw.base.events
,Python需要导入现在导入的draw.base
,因此还没有draw.base
。您可以将import draw.base.utils
替换为import utils
(您也可以在from ..base import utils
中使用2.7 {3.}或from __future__ import absolute_import
中的draw.base.events
)来打破圈子