Confused about Python sub packages

as the name says, I'm confused about sub-subpackages. My package structure is as follows:

draw \ __init__.py base \ __init__.py utils.py events.py master.py basegui.py 

Now the first line of draw.base.events follows:

 import draw.base.utils as _utils 

And the first line of draw.base :

 from draw.base.events import Event, RenderEvent, InputEvent, MouseEvent, KeyboardEvent 

Just check the code for SyntaxErrors with IDLE:

 import draw.base as base 

gives the following 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' 

Can someone explain to me what the problem is?

+4
source share
1 answer

To import draw.base.utils into draw.base.events , Python must import draw.base , which is now being imported, so there is no draw.base . You can replace import draw.base.utils with import utils (you can also use something like from ..base import utils in 2.7, 3.x or with from __future__ import absolute_import ) in draw.base.events to break the circle .

+2
source

Source: https://habr.com/ru/post/1382551/


All Articles