Molitoris
Molitoris

Reputation: 1035

How to activate conda environment in VSCode on Windows 10

Whenever I try to activate conda within VSCode on Windows 10, I get a message to initialize the shell.

I tried the following

I think the problem is within VSCode because I can activate conda from cmd, powershell, gitbash and within the PyCharm terminal.

Do I miss something?

$ conda activate data_science
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

conda info

     active environment : None
       user config file : C:\Users\user\.condarc
 populated config files : 
          conda version : 4.8.3
    conda-build version : 3.18.11
         python version : 3.7.6.final.0
       virtual packages : __cuda=11.0
       base environment : C:\Users\user\anaconda3  (writable)     
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64   
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\user\anaconda3\pkgs
                          C:\Users\user\.conda\pkgs
                          C:\Users\user\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\user\anaconda3\envs
                          C:\Users\user\.conda\envs
                          C:\Users\user\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.8.3 requests/2.22.0 CPython/3.7.6 Windows/10 Windows/10.0.18362
          administrator : False
             netrc file : None
           offline mode : False

Upvotes: 3

Views: 15678

Answers (5)

Erez Shani
Erez Shani

Reputation: 1

You can make a batch file and just open your activated environment with one click. I made an example here https://github.com/Zrt747/condaENVbatch.git

Upvotes: 0

mherzog
mherzog

Reputation: 1230

Per https://stackoverflow.com/a/69127741/11262633, execute conda init. Subsequent runs will then properly execute the needed call to conda.

Upvotes: 2

self.Fool
self.Fool

Reputation: 320

Try this command source activate base, this should work. You can then enter the command conda activate <env_name>

Upvotes: -2

spinningWheel
spinningWheel

Reputation: 39

I had a similar problem, but was not able to find the explicit solution that I needed. I'm couching the cobbled solution that I found that worked for me and hopefully it may work for you if the above answer does not work for you.

One way was to go into the registry and alter the ExecutionPolicy (which I'm not confident in doing).

The way that worked for me was to Press Ctrl+Shift+P in VSCode and open "Preferences: Open Settings (Json)" and add a profile.

This fixed the, "CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'..." error.

Note: The setting here is assuming you are using powershell as the default shell. This also fixed some "& ..." error as well. The error arose because "&" is a PS (powershell) command I believe and if you are using cmd.exe as the shell, the error may pop up.

{...,
"python.pythonPath": "C:\\Users\\<user>\\miniconda3\\envs\\py39\\python.exe",
"python.condaPath": "C:\\Users\\<user>\\miniconda3\\Scripts\\conda.exe",
"terminal.integrated.cwd": "D:\\my_VSCode_storage\\py39_env\\my_project_folder",
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell",
        "python.terminal.executeInFileDir": true,
        "args":["-NoLogo",
                "-ExecutionPolicy",
                "Bypass",
                "-NoExit",
                "-Command",
                "& C:\\Users\\<user>\\miniconda3\\shell\\condabin\\conda-hook.ps1",
                ";conda activate 'C:\\Users\\<user>\\miniconda3'"
                ]
     },
   
     ...}

Upvotes: 1

JarroVGIT
JarroVGIT

Reputation: 5269

Your '$' indicates to me that you are not actually having a Command Line prompt open, but rather an alternative (probably bash?).

Press Ctrl+Shift+P and search for 'Select default shell', change to Command Line prompt and you should be good to go :)

enter image description here

Upvotes: 6

Related Questions