Yes, there are similar questions, but they do not answer my issue. My directory structure is as follows, all __init__.py files are blank.
Package/
__init__.py
sub_package1/
__init__.py
file1.py
sub_package2/
__init__.py
file2.py
In file2.py
I have the following code:
from ..sub_package1 import file1
I get the error mentioned above,
ValueError: attempted relative import beyond top-level package
There are a number of scikit-learn packages which do similar imports and it works for them.
Command that raised the error:
- working directory:
Package/
- Command:
python /path/to/Package/sub_package2/file2.py
Whether or not relative imports work depends on how you invoke the code, unfortunately.
When you
python Package/sub_package2/file2.py
the runtime doesn't recognize thatPackage/sub_package2
is part of the module path. It thinks the module you're working with is justfile2
. So it cannot interpret the..
relative import.When you import the module using its full path, as
python -m ...
does (and as any normal import statement will do), the full import path is recognized and relative imports can be interpreted properly.