Fixing 'Import [module] could not be resolved' in pyright

早过忘川 提交于 2020-07-07 09:41:11

问题


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

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 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?


回答1:


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


来源:https://stackoverflow.com/questions/59108805/fixing-import-module-could-not-be-resolved-in-pyright

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!