VSCode does not recognize venv

Viewed 3650

When I create a new project and the virtual environment using the venv python package, VSCode doesn't recognize the new virtual environment. I follow the bellow instruction:

https://code.visualstudio.com/docs/python/environments

The command that I use in the VSCode integrated terminal is:

python -m venv .venv

The terminal that I use is PowerShell 7, But I tried the CMD terminal too.

After running this command, the .venv folder is created very well on the workspace and I checked its behavior on the terminal.

I tried conda package manager to create a venv and VSCode recognizes it. The problem is only with the venv Python package.

I also tried another Python version to create venv, But the problem still exists.

I read this question:

How can I set up a virtual environment for Python in Visual Studio Code?

I know how to add an environment manually, but I expect VSCode to recognize my environments automatically.

I added these lines to settings.json (Preferences):

"python.venvPath": "~/.venv",
"python.venvFolders": [
    "~/.venv/Scripts"
]

I activated the venv manually using VSCode integrated terminal.

The problem still isn't solved.

This is a screenshot of my problem:

A testing workspace screenshot

I know how to add an environment manually, but I want it to be automatic.

I tried VSCode on another PC, and it worked; It doesn't need anything to do except the presence of venv.

VSCode Version: 1.58.0 Python Extension Version: v2021.6.944021595

4 Answers

The "python.venvPath" should be set to the parent folder of the .venv folder.

Virtual environments located in the folder identified by the python.venvPath setting (see General settings), which can contain multiple virtual environments. The extension looks for virtual environments in the first-level subfolders of venvPath.

But, you need not set that. The Python extension should find the venv environment, as it is just located under your workspace folder.

Virtual environments located directly under the workspace (project) folder.

I tried it locally, it does not work too. It will only show the cached environment path, and the "python.venvPath" does not work either.

And there are some changes of the interpreter storage:

A VSCode internal storage is introduced which will now store the interpreter settings in the workspace & workspace folder scope.

You can refer to here for more details.

So, it's recommended to select the environment path manually for now.

After writing this command in VS CodeTerminal

python -m venv env

You can simply use

.\env\Scripts\Activate

In the same directory path where you have created the virtual enviroment.

For some reason VSCode had uninstalled all my Python extensions including the main Python extension. After reinstalling them it was able to detect my virtualenvs again.

So I was having this same issue and I resolved it by,

1. Open Settings in VSCode.

2. Search for "python.terminal.activateEnvironment"

3. If unchecked/unticked, then check/tick for both "User" and "Workspace".

The problem is not with VSCode but the workspace settings in which I was working on. Just changed that and Voila works like a charm.

Related