问题描述
我正在使用pyright进行类型检查,还使用pytest在Visual Studio Code中进行测试.我的测试的文件夹结构是在程序包root中有一个"test"子文件夹.例如
I'm using pyright for type checking and I'm also using pytest for testing inside Visual Studio Code. The folder structure for my tests is to have a 'test' subfolder in the package root . For example
|
MyPackage
|-- __init__.py
|-- MyModule.py
|--test
|-- __init__.py
|--MyModule_test.py
我正在组织这样的事情,因为将会有很多软件包,并且我想保持事情井井有条.我在pytest内部
I'm organizing things like this as there will be many packages and I want to keep things organized.Inside pytest I have
import pytest
import MyPackage.MyModule
...
Pytest能够发现测试并可以正常运行,因为它具有调整其sys.path
(或其他功能)的特殊功能.但是,pyright只会抱怨无法导入模块,Import 'MyPackage.MyModule' could not be resolvedpyright (reportMissingImports)
.这是有道理的,但是有什么方法可以解决这个问题,无论是在拼写方式还是在Visual Studio Code设置中,都可以防止这种情况的发生?
Pytest is able to discover the tests and run them OK because it has some special ability to adjust its sys.path
(or something).However, pyright will just complain that it cannot import the module,Import 'MyPackage.MyModule' could not be resolvedpyright (reportMissingImports)
. This makes sense, but is there some way to deal with this, either in pyright or in the Visual Studio Code settings to stop this from complaining?
推荐答案
好,如图所示的相对导入此处能够解决此问题.所以我的情况应该是
Ok, a relative import as illustrated here was able to solve this. So in my case I should have
# MyModule_test.py
import pytest
from .. import MyModule
这篇关于在pyright中修复“导入[模块]无法解析"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!