ValueError:导入时尝试了超出顶层包的相对导入

2024-04-26 09:53:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个结构:

.
├── __init__.py
├── folder1
│   ├── __init__.py
│   └── file_will_import.py
└── folder2
  ├── __init__.py
  └── helper.py

文件\u将_导入.py包含以下内容:

from ..folder2 import helper
helper.h1()

助手py有以下功能:

def h1():
    print("Hello")

当我跑的时候 python文件将导入py,它给出的错误是:

Traceback (most recent call last):
  File "file_will_import.py", line 1, in <module>
    from ..folder2 import helper
ValueError: attempted relative import beyond top-level package.

我试过谷歌的所有东西,但都没用。你知道吗


Tags: 文件frompyimport功能helperinitdef