It seems there are already quite some questions here about relative import in python 3, but after going through many of them I still didn’t find the answer for my issue. so here is the question.
I have a package shown below
package/ __init__.py A/ __init__.py foo.py test_A/ __init__.py test.py
and I have a single line in test.py:
from ..A import foo
now, I am in the folder of
package, and I run
python -m test_A.test
I got message
"ValueError: attempted relative import beyond top-level package"
but if I am in the parent folder of
package, e.g., I run:
cd .. python -m package.test_A.test
everything is fine.
Now my question is: when I am in the folder of
package, and I run the module inside the test_A sub-package as
test_A.test, based on my understanding,
..A goes up only one level, which is still within the
package folder, why it gives message saying
beyond top-level package. What is exactly the reason that causes this error message?