'Visual Studio Code does not detect Virtual Environments

Visual Studio Code does not detect virtual environments. I run vscode in the folder where the venv folder is located, when I try to select the kernel in vscode I can see the main environment and one located elsewhere on the disk. Jupyter running in vscode also doesn't see this environment. I have installed ipykernel in this environment. I tried to reinstall vscode and python extension.

I tried to set the path in settings.json inside .vscode:

{
    "python.pythonPath": ".\\venv\\Scripts\\python.exe"
}

Windows 10
Python 3.6.7 (64-bit)
VSCode 1.54.3



Solution 1:[1]

OK, I found a solution. Firstly uninstall Visual Studio Code. Go to C:\Users\Your_profile and delete the folders related to Visual Studio Code that start with a period. Then turn on showing hidden folders and go to C:\Users\Your_profile\AppData. Type vscode in the file finder and remove all foders and files related to Visual Studio Code. Finally, install Visual Studio Code and enjoy the virtual environments. :)

Solution 2:[2]

  1. In VSCode open your command palette — Ctrl+Shift+P by default

  2. Look for Python: Select Interpreter

  3. In Select Interpreter choose Enter interpreter path... and then Find...

  4. Navigate to your venv folder — eg, ~/pyenvs/myenv/ or \Users\Foo\Bar\PyEnvs\MyEnv\

  5. In the virtual environment folder choose <your-venv-name>/bin/python or <your-venv-name>/bin/python3


The issue is that VSCode's Python extension by default uses the main python or python3 program while venv effectively creates a "new" python/python3 executable (that is kind of the point of venv) so the extension does not have access to anything (available modules, namespaces, etc) that you have installed through a venv since the venv specific installations are not available to the main Python interpreter (again, this is by design—like how applications installed in a VM are not available to the host OS)

Solution 3:[3]

VS Code: Python Interpreter can't find my venv

The only solution I found was to delete the venv and recreate it. I followed these steps but I'll provide a brief summary for Windows:

  1. Activate your virtualenv. Go to the parent folder where your Virtual Environment is located and run venv\scripts\activate. Keep in mind that the first name "venv" can vary.
  2. Create a requirements.txt file. pip freeze requirements.txt
  3. deactivate to exit the venv
  4. rm venv to delete the venv
  5. py -m venv venv to create a new one
  6. pip install -r requirements.txt to install the requirements.

This worked for me, I didn't delete the old, but created a new python -m venv /path/newVenv in the ~/Envs folder, C:\Users\Admin\Envs. Maybe VS Code is searching in the ~/Envs folder, or it needs to be added to the python.path in the View -> Command Pallete -> >Preferences: Open User Settings.

Solution 4:[4]

None of the suggestions on this thread worked for me. That said, I don't think the issue lies with VS Code, it's venv. I wound up installing PyCharm to fix this. After you’ve downloaded:

PyCharm > Preferences > search “interpreter” > Project: Python Interpreter > Click ‘+’ > in Virtualenv Environment > New environment (should automatically populate everything for a new env). Select OK, OK, OK.

In the bottom left, you’ll see Git | TODO | Problems | Terminal…etc. Click “Terminal” and you should see your environment already activated. From there, pip3 install your dependencies. Close PyCharm.

Go back to VS Code, open your project, and follow the suggestions above to select the Virtualenv (mine was 'venv': venv) as your interpreter.

Finally resolved.

Solution 5:[5]

If you're a Linux user, and you've used this or similaar to create your virtual environment:

python3 -m venv venv

and you cannot get the debug to work, remove your venv and create it from the VS Code terminal (click Ctrl + back-tick to open).

When you create it from the VS Code terminal, VS Code will ask if you want to use this new environment it amazingly detected for this workspace, say yes.

Solution 6:[6]

Part of the confusion here may stem from UI behavior that is at odds with the VScode documentation. The docs state:

When you create a new virtual environment, a prompt will be displayed to allow you to select it for the workspace.

That didn't happen in my case (VScode 1.66.2 running on Windows 10 with Remote - WSL plugin version 0.66.2). I followed the steps outlined here; I did not see the pop-up described by the VScode docs but clicking on the Python interpreter version in the status bar showed that VScode had automatically selected the interpreter installed in the virtual environment. Furthermore, I did observe that VScode was sourcing .venv/bin/activate as described in the post linked above

Run the code by clicking the play button, note the .venv and source “/Users/jemurray/Google Drive/scripts/personalPython/helloworld/.venv/bin/activate” in the terminal shows the script is activated and running in the virtual environment

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ZarakshR
Solution 3 DMinovski
Solution 4
Solution 5 beep_check
Solution 6 cjobrien