Ant*_*nty 6 python pytest visual-studio-code pyright
我使用 pyright 进行类型检查,我还使用 pytest 在 Visual Studio Code 中进行测试。我的测试的文件夹结构是在包 root 中有一个“test”子文件夹。例如
|
MyPackage
|-- __init__.py
|-- MyModule.py
|--test
|-- __init__.py
|--MyModule_test.py
Run Code Online (Sandbox Code Playgroud)
我正在组织这样的事情,因为会有很多包,我想让事情井井有条。在 pytest 我有
|
MyPackage
|-- __init__.py
|-- MyModule.py
|--test
|-- __init__.py
|--MyModule_test.py
Run Code Online (Sandbox Code Playgroud)
Pytest 能够发现测试并运行它们,因为它有一些特殊的能力来调整它的sys.path(或其他东西)。但是,pyright 只会抱怨它无法导入模块
Import 'MyPackage.MyModule' could not be resolvedpyright (reportMissingImports). 这是有道理的,但是有没有办法解决这个问题,无论是在 pyright 还是在 Visual Studio Code 设置中,以阻止它抱怨?
好的,如图所示的相对导入能够解决这个问题。所以就我而言我应该
# MyModule_test.py
import pytest
from .. import MyModule
Run Code Online (Sandbox Code Playgroud)