visual-studio-codepylance

Python language features do not work with libraries installed in Poetry local virtual environment (.venv)


I have a Python Poetry project. Visual Studio Code features like auto-import and auto-complete do not work for the project dependency libraries. Pylance (the Python language server for Visual Studio Code) does not find dependencies, and they are highlighted with red underlining.

enter image description here

Python interpreter is correctly configured and selected. How to fix?

In the Visual Studio Code console Pylance reports:

2025-08-03 08:25:50.202 [info] (6995) Auto-excluding **/node_modules
2025-08-03 08:25:50.202 [info] (6995) Auto-excluding **/__pycache__
2025-08-03 08:25:50.202 [info] (6995) Auto-excluding **/.*
2025-08-03 08:25:50.202 [info] (6995) Assuming Python version 3.11.10.final.0
2025-08-03 08:25:50.293 [info] (6995) Found 3 source files

Solution

  • Since some Pylance versions, as writing of this in 2025-08, there is an option to auto-exclude folders, namely **/.*. This matches all Poetry installations using locally managed virtual environment which lies in the folder .venv. Thus Pylance, ignores any dependencies in Poetry virtual environment installation by default.

    You can fix this by adding pyright section in pyproject.toml file for your Poetry. [pyright] section allows you to set some (not all) Pylance settings. Here we redefine the exclusion list so that .venv is included:

    [tool.pyright]
    include = [".venv"]
    exclude = [".git"]
    

    Restart Visual Studio Code and Pylance should read these settings and index files correctly. You see this in the language server output:

    2025-08-03 08:25:44.240 [info] (6995) Found 11754 source files
    

    Now dependencies will show up correctly (no red underlining):

    enter image description here

    For large Python codebases with a lot of dependencies you may also hit the limit of Pylance indexed files which is set to 2000 by default. You will see this warning in Python Langauge Server output:

    (34964) BG: Workspace indexing has hit its upper limit: 2000 files
    

    You need to change the setting userFileIndexingLimit to a higher value like 15000:

    enter image description here

    You may also need to try the command Python: Clear persistent indicies from Visual Studio Command palette to force Pylance to find files.