python 3.x - ValueError: attempted relative import beyond top-level package -
i playing the python's import system in order understand better how works, , encountered problem. have following structure
pkg/ __init__.py c.py d.py subpkg/ __init__.py a.py b.py
inside a.py
have following code:
from . import b .. import d
and inside c.py
have following:
import subpkg.a
now receive following error:
valueerror: attempted relative import beyond top-level package
but why? how can solve it? running c.py
idle, , pkg
should considered package, since has __init__.py
file.
the first import works fine, it's following doesn't work:
from .. import d
because attempting import parent package, apparently cannot, weird reason.
python 3 changed import system every time want module around 1 working, need relative imports (unless mess python_path
or sys.path
).
the correct usage here should be
from .subpkg import
when working idle, have totally different environment. therefore, add current location path imports work again.
try:
sys.path.insert(0, '')
it might weird, greater good
ps: if last thing not work -- don't have idle environment right -- because work directory set wrong.
try answer instead: https://stackoverflow.com/a/17361545/754991
Comments
Post a Comment