对Python子子包感到困惑

时间:2011-11-22 18:30:29

标签: python module packages

正如标题所说,我对子包装感到困惑。我的包结构如下:

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'

有人可以向我解释这个问题吗?

1 个答案:

答案 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)来打破圈子