Multilevel relative import
I have following folder structure
top\
__init__.py
util\
__init__.py
utiltest.py
foo\
__init__.py
foo.py
bar\
__init__.py
foobar.py
I want to access from foobar.py
the module utiltest.py
. I tried following relative import, but this doesn't work:
from ...util.utiltest import *
I always get
ValueError: Attempted relative import beyond toplevel package
How to do such a multileve relative import?
You must import foobar
from the parent folder of top
:
import top.foo.bar.foobar
This tells Python that top
is the top level package. Relative imports are possible only inside a package.