如何在Python中相对导入父目录中文件夹中的文件

2024-06-17 13:00:45 发布

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

我有一个大致如下的文件系统:

> unit-testing
   __init__.py
   > hardware
      __init__.py
      test_All.py
   > tests
      __init__.py
      scc.py

我正在尝试将scc.py导入test_ALL.py文件,如下所示:

from ..tests import scc

这将导致以下错误:

"ValueError: attempted relative import beyond top-level package"

这是令人惊讶的,因为this part of the Python 3.7.1 documentation有一个与我的代码语法相同的示例:

from ..filters import equalizer

所以我不确定为什么会发生这个错误,或者它到底意味着什么。有什么建议吗


Tags: 文件frompytestimportinit错误tests