hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Hatch venvs are not detected automatically by VSCode

Open foxx opened this issue 3 years ago • 5 comments

The Problem

It would seem that Hatch is not yet in the defined list of virtualenv providers here.

The default location for all virtualenvs created by Hatch appears to be:

~/.local/share/hatch/env/virtual/

Temporary Workaround

As a temporary workaround, one can set the following global parameter in ~/.config/hatch/config.toml.

[dirs.env]
virtual = "~/.virtualenvs"

This will result in VSCode correctly detecting the virtualenv.

Long Term Solution

It would be nice if this all worked out of the box, which presumably can only be done if support for hatch is added as an official venv provider into vscode-python. If possible, could the maintainers of Hatch confirm if this is correct, and if so, give some clues as to how difficult it would be to make this happen?

I'd be happy to donate a couple of days of engineering time from our team if it's a simple enough fix.

Related

Possibly related to #596.

foxx avatar Nov 13 '22 15:11 foxx

I would love help adding support to VSCode! The other common setup is .hatch https://hatch.pypa.io/latest/blog/2022/10/08/hatch-v160/#virtual-environment-location

ofek avatar Nov 13 '22 16:11 ofek

Also helpful: https://hatch.pypa.io/latest/cli/reference/#hatch-env-find

ofek avatar Nov 13 '22 16:11 ofek

The "here link" in https://github.com/pypa/hatch/issues/600#issue-1446978225 (the first comment) seems to point to some out of date env discovery code. The right place to start adding this functionality today might be by creating a hatch.ts file here:

https://github.com/microsoft/vscode-python/tree/main/src/client/pythonEnvironments/common/environmentManagers

The poetry.ts file that's already there might be a good example to build on.

greyltc avatar Dec 12 '22 12:12 greyltc

[dirs.env] virtual = "~/.virtualenvs"

This will result in VSCode correctly detecting the virtualenv.

It would be good to add this little bit to the docs. It took me a long time to find this issue, to get to this solution. I looked at https://github.com/pypa/hatch/blob/master/docs/config/hatch.md with an eye to do that but backed out because a reference doc might not be the right place to put a tool specific notation.

Next place I thought of is the 1.6 blog announcement, since that's the page the doc search results for 'hatch' and 'vscode|Visual Studio Code' brings me to most, but that'd be changing history. Maybe as a comment to the bottom of the page?

maphew avatar Apr 29 '23 05:04 maphew

@maphew, having had the same issue I stumbled upon a better solution in #596 by @OldGrumpyViking. Even though he's old and grumpy, the viking seems to be right, that

hatch config set dirs.env.virtual .direnv

is a better solution, as it avoids name clashes in case you are using the same environment names in several hatch projects, which is quite likely actually.

I also mention this setting now in the hatchlor.

FlorianWilhelm avatar Nov 11 '23 21:11 FlorianWilhelm

@FlorianWilhelm FYI, as an alternative to hatch config set dirs.env.virtual .direnv, if you want to use .hatch (as suggested in https://hatch.pypa.io/latest/config/hatch/#environments):

hatch config set dirs.env.virtual .hatch

Then, in VS Code, set python.venvPath to ${workspaceFolder}/.hatch (I had to restart VS Code for this setting to get picked up). This will tell VS Code to look for virtual environments in the .hatch folder in the workspace.

cdwilson avatar Mar 26 '24 06:03 cdwilson

https://code.visualstudio.com/updates/v1_88#_hatch-environment-discovery

ofek avatar Apr 06 '24 17:04 ofek

https://code.visualstudio.com/updates/v1_88#_hatch-environment-discovery

Are there details how to make this work? This issue suggests hatch needs to be configured to create the virtual env(s) under the project directory: https://github.com/microsoft/vscode-python/issues/22810.

My use-case is a bit more difficult since it's a monorepo with multiple hatch environments, per service, each having totally different dependencies. By default, the hatch virtual envs are rooted under: /home/vscode/.local/share/hatch/env/virtual. (VSCode dev-container). Is there such thing as a VSCode extension to offer a drop-down list of envs to use as "interpreter"?

wolfch-elsevier avatar May 14 '24 22:05 wolfch-elsevier

https://hatch.pypa.io/latest/how-to/integrate/vscode/

ofek avatar May 15 '24 00:05 ofek

Thanks @ofek, I can confirm that with newer VSC versions this works perfectly also with hatch's default settings. Thanks for providing the docs.

FlorianWilhelm avatar May 15 '24 06:05 FlorianWilhelm

Hi @ofek thanks for providing the doc link. For me, the envs created by Hatch do not appear in the Python: Select Interpreter drop-down list - I have to select Enter interpreter path, then Browse your file system to find a Python interpreter., then point-click navigate to the Hatch venv bin/python. Once I do do that, only then does that virtual env. appear in the Python: Select Interpreter drop-down list.

Unlike other users, maybe the problem I'm having is because my project is a monorepo with multiple Hatch environments and I'm doing everything inside a dev container?

VSCode: 1.89.1 OS: MacOs 13.5 Python: 3.11.9 Hatch is installed globally (inside the dev-container):

$ which hatch
/usr/local/python/current/bin/hatch

wolfch-elsevier avatar May 15 '24 15:05 wolfch-elsevier