44

I read this Stack Overflow post on a similar issue, but the suggestions there don't seem to be working. I installed Visual Studio Code on my Windows machine and added the Python extension. Then I changed the Python path for my project to C:\Users\username\.conda\envs\tom\python.exe. The .vscode/settings.json has this in it:

{
    "python.pythonPath": "C:\\Users\\username\\.conda\\envs\\tom\\python.exe"
}

The status bar in Visual Studio Code also shows:

Enter image description here

But when I do conda env list even after doing conda activate tom in the terminal I get the output:

# conda environments:
#
base                  *  C:\ProgramData\Anaconda3
tom                      C:\Users\username\.conda\envs\tom

Instead of:

# conda environments:
#
base                     C:\ProgramData\Anaconda3
tom                   *  C:\Users\username\.conda\envs\tom

Also the packages not installed in base don't get imported when I try python app.py. What should I do?

where python runs, but it doesn't give any output.

Also,

import os
import sys
os.path.dirname(sys.executable)

gives

'C:\\Python38'
  • 1
    I have the same issue. It used to work before and I did not have start VS code from the conda prompt ever. Also, the correct python.exe (from the env) is used, just the dependencies are not found. – Jann Poppinga Nov 11 '20 at 14:42
  • 2
    Could the issue be PowerShell is default shell in VSCode, but miniconda, miniforge, and mambaforge only work with CMD.EXE (on windows)? When .bat files are run from PowerShell instead of CMD shell the env vars are set but then tossed when CMD.EXE process immediately exits, never updating the PowerShell environment. – yzorg Jul 01 '21 at 14:53

15 Answers15

45

First, open the Anaconda prompt (How to access Anaconda command prompt in Windows 10 (64-bit)), and type:

conda activate tom

To activate your virtual environment.

Then to open Visual Studio Code in this active environment, type

code

And it should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • 2
    No I didn't get any error... It opened vscode, i added my project and the tried ```conda env list``` in the terminal... I am still getting base as my activate env.. – Sabito stands with Ukraine May 24 '20 at 12:47
  • It could be due to the way VSCode was installed. Try to uninstall it, and re-install from the Anaconda-Navigator – Gustav Rasmussen May 24 '20 at 12:58
  • You can do a uninstall and then reinstall of the Anaconda-Navigator, and then VSCode will be available to install from there again. The previous removal of VSCode might have been the Anaconda installation, it needs to be the system installation. This should work. – Gustav Rasmussen May 24 '20 at 20:11
  • Hm... I was afraid you might say this... I will wait for a couple of days... If I don't find any alternative or solution then I guess I will a reinstall.. – Sabito stands with Ukraine May 24 '20 at 20:35
  • 1
    If you had installed many packages specifically into your virtual environment, you can run the command: conda list, and copy the package names and versions to a file for easier reinstall. (will only show packages installed with conda, for pip installs you must open the actual installation folder to see the packages). – Gustav Rasmussen May 24 '20 at 20:55
  • What if I copy the package folders into a pen-drive, remove anaconda and then reinstall it, but this time on my D drive instead of my C drive... Will pasting the folders in my `envs` directory work? – Sabito stands with Ukraine May 25 '20 at 04:58
  • 1
    I tried it, but just with few packages. But you could run into dependency resolution issues I fear. However, reinstalling packages won't take up that much time I think. As long as you record the current state of both package name and version it should be a smooth process. – Gustav Rasmussen May 25 '20 at 06:33
35

I was facing the same issue for a long time and nothing seemed to work. Out of nowhere, VS Code suggested me the following in a notification prompt:

We noticed you're using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we recommend that you let the Python extension change "terminal.integrated.inheritEnv" to false in your user settings.

I clicked yes on it and worked. You can also manually set this in your settings.json as follows:

  1. Press Shift + Command + P to open command palette.
  2. Type settings.json and select `Preferences: Open Settings (JSON)
  3. In the JSON file, add the key-value pair "terminal.integrated.inheritEnv": false
  4. Save the JSON file
  • 1
    Similar situation here, but I had to create a new Terminal from the Terminal Menu for it to work. Exiting VS Code, activating the environment, etc. had no effect. Some details here: https://code.visualstudio.com/docs/python/environments#_environments-and-terminal-windows – atlas_scoffed Nov 23 '21 at 21:57
28
  1. In Vscode hit ctrl+` to open your terminal.
  2. Then within the terminal type: conda init.
  3. Close and reopen the terminal.
  4. Use Conda normally.
  • 2
    This worked for me, is a permanent fix, and fixed another issue with VS Code not finding conda. One effect of running `conda init` is to add the `condabin` folder to PATH, which puts `conda.bat` on the path, but not python (see @peter-c answer) – Simon Judd May 27 '21 at 13:48
  • 2
    Running conda init printed out some checks and a final "Nothing changed" but it did fixed that for me. I guess nothing needed to change. – Felício Jun 11 '21 at 12:50
  • 1
    IMO this is the best approach. For people with `QWERTZ` (ger, aus, ..) keyboard layout, replace the shortcut in the first step with `ctrl`+ `ö` – Björn Nov 16 '22 at 02:16
  • if you are just playing around this might have unintended consequences after miniconda/anaconda uninstall, to fix it you might need to fiddle with registry: https://stackoverflow.com/questions/66335300/cmd-crashes-with-exit-code-1-after-uninstalling-anaconda – Aurimas Stands with Ukraine May 15 '23 at 12:48
19

enter image description here

I was helped by switching the terminal from powershell to cmd

user2281726
  • 309
  • 2
  • 5
6

I've already tried most solutions to solve this problem. However, those didn't work. Following the steps that Sabito 錆兎 mentioned in their answer, I finally solved it.

Solution 1:

  1. ctrl+shift+p

  2. Type terminal: select default profile

  3. Choose Command Prompt.

    Image for 'choose Command Prompt'

  4. Open a new terminal and you can use cmd to do such things.

Another way to change the default terminal is (in case you forget the command):

  1. On the right top of the terminal panel, click + (Launch Profiles)
  2. Select default Profile
  3. Choose which you want in this way, you can also open configure terminal settings

Solution 2:

  1. Do what Sabito 錆兎 said in their answer.

  2. ctrl+shift+p

  3. Type Python: Select Interpreter

  4. Choose which env in conda you need. You can also select on the bottom panel.

  5. Unnecessary step: in vscode-settings.json, make sure that if this line exists then the argument is true:

    "python.terminal.activateEnvironment": true,
    

    Now, vscode will automatically change your env in your terminal according to your choice, like this:

    CMD

    When I choose base:conda, I open a new terminal. In the console, it shows

    F:\GitHub\t>E:/Anaconda/Install/Scripts/activate 
    (base) F:\GitHub\t>conda activate base 
    (base) F:\GitHub\t>
    

    When I choose python3.8:condait shows

    F:\GitHub\t>E:/Anaconda/Install/Scripts/activate 
    (base) F:\GitHub\t>conda activate python3.8 
    (python3.8) F:\GitHub\t>
    

    PowerShell

    When I choose python3.8:conda. it shows

    (base) PS F:\GitHub\t> conda activate python3.8 
    (python3.8) PS F:\GitHub\t>
    

    When I choose base:conda, it shows

    (base) PS F:\GitHub\t> conda activate base  
    (base) PS  F:\GitHub\t>
    
cooooopd
  • 71
  • 1
  • 4
2

Activating anaconda virtual environment in vs code

  1. Go to the menu bar and click on Terminal.
  2. Type: conda init
  3. Close this terminal and open a new one
  4. In the new terminal window, Type conda info --envs
    (Your conda virtual env should be there in the list)
  5. Type conda activate name_of_venv
  • 4
    If the Terminal isn't properly configured, the command ```conda init``` will not work because ```conda``` is not in the search path. On my system, it is in ```C:\Users\\Anaconda3\Scripts```. You therefore may need to use ```C:\Users\\Anaconda3\Scripts\conda init```. – mherzog Oct 20 '21 at 18:28
2
  1. Type: conda init
  2. switch to cmd terminal, because the shell terminal has problems
  3. enjoy

it's work for me in vscode

1

This seems to be because the Anaconda installation recommends not changing the windows PATH to reference anaconda, as it can clash with other installations, I had no problems when my PATH variable was modified. It seems like vs code (or most likely the Python extension) is still not activating anaconda correctly itself.

Peter C
  • 2,257
  • 2
  • 25
  • 28
1

The virtual environment can be activated in the VSCode terminal, but the Python version is not switched.

The solution is to select “bash” in VSCode, everything is alright.

lorinlin
  • 11
  • 1
0

If conda init fails, it might not just be a problem of conda missing in the search path. In my case it was because PowerShell did not have the rights to load the profile.ps1, see The term 'conda' is not recognized as the name of a cmdlet.

In that case, and if you have not yet done so, install PowerShell 7 in parallel to PowerShell 5 and fill the C:\Users\USER\Documents\PowerShell\profile.ps1 with:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

enter image description here

Then add the new PowerShell 7 terminal to vscode, see How to add anaconda powershell to vscode?.

The terminal menu:

enter image description here

And conda init and the activation of the chosen Python interpreter's environment that you see in the status bar of vscode will run automatically any time you open PowerShell 7.

questionto42
  • 7,175
  • 4
  • 57
  • 90
0

PythonPath is no longer used by the default Python plugin now (as of 2021). Instead, you can add the interpreter path using the following:

  1. In a terminal, run the following to get the path to your env:
conda activate <name of your env> && which python
  1. Then set the VSCode JSON setting (either user or workspace):
{
    "python.defaultInterpreterPath": "<Path to your env>",
}
  1. Restart VSCode completely to see your env in the terminal, as it seems that VSCode sometimes caches the terminal window.

(All this assumes that there are no other messed up settings in your vscode. If there are, look at other answers to this question).

Gamrix
  • 661
  • 4
  • 12
0

There seems to be many different causes for conda not being automatically activated. I'm using python in wsl2 with bash as my shell and setting the following two settings to true fixed it for me: enter image description here

owennewo
  • 301
  • 2
  • 3
0

In my case when you:

  1. installed Python and set it to the Windows PATH
  2. refused to add Anaconda to PATHs as default Python interpreter during installation
  3. vs code finds the conda, but not conda cmdlet

Solution:

Let vs code add the conda directory as the first search path in Windows PATH for you when it is running. This can be done both for the whole vs code and for a specific workspace.

Entire vs code:

  1. Ctrl + Shift + P
  2. Preferences: Open User Settings (JSON)
  3. add the path to the location of your conda.exe, in my case it's C:\Users\USERNAME\AppData\Local\anaconda3\Scripts\
 "terminal.integrated.env.windows": {
        "PATH": "C:\\Users\\USERNAME\\AppData\\Local\\anaconda3\\Scripts\\;${env:PATH}"
    }

Specific workspace:

  1. Ctrl + Shift + P
  2. Preferences: Open Workspace Settings (JSON)
    "settings":
    {
        "terminal.integrated.env.windows": {
            "PATH": "C:\\Users\\USERNAME\\AppData\\Local\\anaconda3\\Scripts\\;${env:PATH}"
        }
    }
0

thanks so much. I did work as tutorial and it run

I've already tried most solutions to solve this problem. However, those didn't work. Following the steps that Sabito 錆兎 mentioned in their answer, I finally solved it.

Solution 1:

ctrl+shift+p

Type terminal: select default profile

Choose Command Prompt.

thanks a lot

  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/34804374) – XMehdi01 Aug 10 '23 at 22:24
0

In my case simply choosing "Command Prompt" instead of "Poweshell" as VS-code terminal worked. I am not sure if there are any disadvantages of using command prompt over Powershell because I am not a heavy windows user. Not for python programming at least. I will be exploring more and will add edits later if I found something.

abggcv
  • 461
  • 5
  • 20