ValueError: attempted relative import beyond top-level package
时间: 2024-02-12 11:34:21 浏览: 272
This error occurs when you try to use a relative import outside of a package.
For example, let's say you have a directory structure like this:
```
my_package/
__init__.py
module1.py
subpackage/
__init__.py
module2.py
```
If you try to use a relative import in `module2.py` to import `module1.py`, you will get the `ValueError`:
```
from ..module1 import some_function # causes ValueError
```
This is because `module2.py` is already at the top level of the package, so there is no parent package to import from.
To fix this error, you can use an absolute import instead:
```
from my_package.module1 import some_function
```
This will work regardless of where the module is located within the package.
阅读全文