Pyright with multiple venv (monorepo)

Question:

Got an issue with pyright on a monorepo: it didn’t recognize classes/functions imported from nearby projects/libraries (get a reportMissingImports).

For instance, with this repo structure:

.
├── library
|   └── src
|       └── __init__.py
├── project1
|   └── src
|       └── main.py
├── project2 ...
└── pyproject.toml

This line in main.py will raise the reportMissingImports error (even if the code is working):

from library import ModuleClass

As a manual workaround, this command works from root level:

poetry run pyright --pythonpath project1/.venv/bin/python project1/main.py

But as I have multiple projects, each with its own .venv folder, I can’t set pythonpath globally.

I tried multiple options in pyproject.toml to configure pyright properly for the whole repository, but nothing worked so far.
What is the proper way to configure multiple venv like that with pyright ?

FYI, as a context, my final goal is to setup pyright for sublime text 3 with LSP-pyright and python >=3.11. But at least command line should work before that (-‿-")

Asked By: Romain

||

Answers:

Multiple things here :

  • Sublime text project needs to target the root dir. I used to open my project by adding each subdir, but in fact its a bad idea ;
  • LSP-pyright seems to also read pyproject.toml config at root level (not precised in its documentation) ;
  • my final pyrightconfig.json file is pretty simple:
{
    //"verboseOutput": true,
    "reportMissingImports": "none",
    "reportMissingModuleSource": "none",
    "executionEnvironments": [
        {
          "root": ".",
          "extraPaths": ["library/src", "project1/src", "project2/src"]
        }
    ]
}

extraPaths is resolved from root dir. It needs the src/ subdir declaration.

Answered By: Romain