pythonvisual-studio-codepylancehatch

Visual Studio Code with Pylance cannot resolve imports while using hatch


When I created my project using hatch new name imports were resolved correctly. Now that I opened it again I get yellow squigly line under each import with error in tooltip:

from django.conf import settings # -> Import "django.conf" could not be resolved from source Pylance(reportMissingModuleSource)

I understand this error happens when vscode finds wrong python executable (usually global one instead of venv one). So when not using hatch I could resolve it by creating .vscode/settings.json file with content like:

{
  "python.defaultInterpreterPath": "path/to/venv"
}

However in this project I am using hatch build tool, which manages environments itself (at least I cannot find them in project directory). How do I point vscode to correct python interpreter in this case?

Edit:

I tried changing venv location by adding dirs.env to my pyproject.toml:

[dirs.env]
virtual = ".hatch"

Then I deleted existing default environment using hatch env prune and creating it again using hatch env create. However hatch env find default still shows old location and .hatch was not created:

$ hatch env find default
C:\Users\Matija\AppData\Local\hatch\env\virtual\cq\bnqHl4TX\cq

Adding new environment to pyproject.toml and creating it using hatch env create vsc also creates it in AppData instead of in .hatch:

In pyproject.toml:

[tool.hatch.envs.vsc]

Commands:

$ hatch env create vsc
$ hatch env find vsc
C:\Users\Matija\AppData\Local\hatch\env\virtual\cq\bnqHl4TX\vsc

Solution

  • For me, using the below solved it:

    [tool.hatch.envs.default]
    path = ".hatch"
    

    instead of

    [dirs.env]
    virtual = ".hatch"
    

    I think [tool.hatch.envs.default] has a higher priority than [dirs.env].