我在 Pycharm 中有以下项目结构 -
project/
src/
main.py
config/
__init__.py
settings.py
package1/
__init__.py
client.py # Imports settings from config using -> from ..config import settings
tests/
__init__.py
test.py
所以这里我的 client.py 导入设置 -
from ..config import settings
main.py 导入客户端 -
from package1.client import clientFunction # ClientFunction is the method defined inside client.py
我使用 pytest 来运行测试。所以现在当我运行 pytest 配置时,在我的测试用例中我导入了 main。我在我的代码中没有看到任何错误,但是当测试运行时它说 -
from ..config import settings
E ValueError: attempted relative import beyond top-level package
我该如何解决这个问题?似乎 pytest 采取了不同的路径来执行测试,因此相对导入超出了顶层。