简体   繁体   English

在 pyright 中修复“无法解析导入 [模块]”

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

I'm using pyright for type checking and I'm also using pytest for testing inside Visual Studio Code.我使用 pyright 进行类型检查,还使用 pytest 在 Visual Studio Code 中进行测试。 The folder structure for my tests is to have a 'test' subfolder in the package root.我测试的文件夹结构是在 package 根目录中有一个“测试”子文件夹。 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里面 pytest 我有

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). Pytest 能够发现测试并正常运行它们,因为它有一些特殊的能力来调整它的sys.path (或其他东西)。 However, pyright will just complain that it cannot import the module, Import 'MyPackage.MyModule' could not be resolvedpyright (reportMissingImports) .但是,pyright 只会抱怨它无法导入模块, 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?这是有道理的,但是有什么方法可以解决这个问题,无论是在 pyright 中还是在 Visual Studio Code 设置中都可以阻止这种抱怨?

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

You can add the library path to the path variable.您可以将库路径添加到路径变量。

import sys
sys.path.insert(1, str('..'))
import MyModule

To enable Pylance to use your library properly (for auto-complete ...), use the following steps:要使 Pylance 能够正确使用您的库(用于自动完成...),请使用以下步骤:

Pylance, by default, includes the root path of your workspace.默认情况下,Pylance 包含工作区的根路径。 If you want to include other subdirectories as import resolution paths, you can add them using the python.analysis.extraPaths setting for the workspace.如果要包含其他子目录作为导入解析路径,可以使用工作区的python.analysis.extraPaths设置添加它们。

  1. In VS Code press +<,> to open Settings.在 VS Code 中按 +<,> 打开设置。
  2. Type in python.analysis.extraPaths输入python.analysis.extraPaths
  3. Select "Add Item"选择“添加项目”
  4. Type in the path to your library `..'输入库的路径“..”

You should create a pyrightconfig.json file or pyproject.toml file at the root of your project.您应该在项目的根目录下创建一个pyrightconfig.json文件或pyproject.toml文件。 For example, if it's a Django project, you should have one of those files where manage.py is placed.例如,如果它是一个 Django 项目,您应该拥有放置manage.py的那些文件之一。 Then, set include parameter and add the subdirectories (or app folders in Django terms).然后,设置include参数并添加子目录(或 Django 术语中的 app 文件夹)。

You can consult this sample config file .您可以查阅此示例配置文件 See this issue ticket.请参阅问题单。

For example, if this were my project structure:例如,如果这是我的项目结构:

├── manage.py
├── movie
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── moviereviews
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── pyproject.toml

my pyproject.toml would be:我的pyproject.toml将是:

[tool.pyright]
include = ["movie", "moviereviews"]

If you are working within a Python virtual environment, set venvPath and venv.如果您在 Python 虚拟环境中工作,请设置 venvPath 和 venv。 Consult the documentation for an exhaustive list of options.请查阅文档以获取详尽的选项列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM