VSCode does not recognize venv(Python)

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.

I really don’t know what else I can do. Please help me.

VSCode Version: 1.58.0 Python Extension Version: v2021.6.944021595

Answer

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.