7

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

pppery
  • 3,731
  • 22
  • 33
  • 46
Erfan Rasti
  • 83
  • 1
  • 6
  • Shouldn't your `python.venvPath` be `.\.venv` and `python.venvFolders` be `".\.venv\Scripts"`? You could also try set `python.venvPath` to `${workspaceFolder}\.venv` and resolve from the venv path from the workspace location. – Oluwafemi Sule Jul 12 '21 at 10:16
  • Thanks for your attention @OluwafemiSule . I tried your code lines: `"python.venvPath": "${workspaceFolder}\\.venv",` `"python.venvFolders": [ ".\\.venv\\Scripts" ]` But the problem is not solved. – Erfan Rasti Jul 12 '21 at 10:56

6 Answers6

4

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.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • 1
    @Erfan Rasti I submitted an issue in the GitHub: https://github.com/microsoft/vscode-python/issues/16665 – Steven-MSFT Jul 13 '21 at 08:03
  • Thank you for your complete explanation. I think it's a bug in the new update of VSCode. @Steven-MSFT – Erfan Rasti Jul 13 '21 at 08:25
  • 1
    @Erfan Rasti Yes, It looks like a problem with the Python extension. – Steven-MSFT Jul 13 '21 at 08:27
  • 1
    Good to see that it's not only happening to me. I spent too much time trying to fix that. Meanwhile, what I've done is to add a workspace setting `python.defaultInterpreterPath` set to the path of the venv interpreter and it works for me. But it would be easier if the automatic detection worked... – jotwo Aug 11 '21 at 16:10
  • 2
    The bug discussed here has probably been fixed. I voted for this answer because I was able to solve my problem by setting the `python.venvPath` variable mentioned in this answer in VS Code v1.71.2 settings (macOS). – Alper Sep 18 '22 at 20:06
2

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.

Anirudh Thakur
  • 101
  • 1
  • 1
  • 5
  • 1
    Also within settings.json, you can set this property `"python.terminal.activateEnvInCurrentTerminal": true` to do this for you. Note that the Python extension has to be loaded first. – Jason Cook Jul 12 '21 at 11:22
  • As I mentioned before, I tried your command but it didn't work. @AnirudhThakur Thanks – Erfan Rasti Jul 12 '21 at 13:11
  • I tried your JSON line too, but it didn't work. @JasonCook Thank you – Erfan Rasti Jul 12 '21 at 13:12
0

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.

user21398
  • 419
  • 6
  • 13
0

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.

UrbanConor
  • 162
  • 1
  • 5
  • 16
Master
  • 29
  • 1
  • 10
0

In my case I was using flatpak. I thought giving permission to use just the project directory was enough, but I needed to give vscode permission to my entire home directory (I guess because it needed to read the home-linked python executable).

If you're using flatseal:

enter image description here

Or if you want to use the command line:

# for vscode:
$ flatpak override com.visualstudio.code --filesystem=${HOME}
# for vscodium (which has better privacy than VSCode BTW  )
$ flatpak override com.vscodium.codium --filesystem=${HOME}
Dash2TheDot
  • 109
  • 1
  • 5
0

One Quick Solution

  1. In VSCode Search for (ctrl+shift+p) Python: Select Interpretor
  2. Then set the correct Interpreter path
  3. Browse and select Python executable under your venv. e.g. (\venv\Scripts\python.exe)

More details here

Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76