8

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?

Anti-Distinctlyminty
  • 2,615
  • 5
  • 22
  • 24

3 Answers3

3

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, by default, includes the root path of your workspace. If you want to include other subdirectories as import resolution paths, you can add them using the python.analysis.extraPaths setting for the workspace.

  1. In VS Code press "ctrl" + "," keys to open Settings.
  2. Type in python.analysis.extraPaths
  3. Select "Add Item"
  4. Type in the path to your library `..'
Ryan
  • 1,247
  • 1
  • 10
  • 12
2

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
Anti-Distinctlyminty
  • 2,615
  • 5
  • 22
  • 24
  • 1
    I have the same problem but I can't use what you have. I have a statement like this in my pytest test file: `from quendor.zmachine.header import StoryHeader` This works fine in that PyTest can handle the import. Pyright refuses to find it. And a relative import doesn't work at all, unfortunately. My only solution has been to disable Pyright. – Jeff Nyman Jun 07 '20 at 18:41
2

You should create a pyrightconfig.json file or pyproject.toml file at the root of your project. For example, if it's a Django project, you should have one of those files where manage.py is placed. Then, set include parameter and add the subdirectories (or app folders in Django terms).

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:

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

If you are working within a Python virtual environment, set venvPath and venv. Consult the documentation for an exhaustive list of options.

harsath
  • 699
  • 1
  • 7
  • 8