embradley
embradley

Reputation: 485

VSCode conda activate base giving CommandNotFoundError

I have Anaconda and Visual Studio Code installed on my computer. My default terminal for VS Code is Git Bash. When I open a new terminal in VSCode, it immediately runs the following commands:

C:/Users/ethan/AppData/Local/Continuum/anaconda3/Scripts/activate
conda activate base

The second of these commands gives the following error:

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'.

I have tried running conda init bash and conda init --all both inside the VSCode terminal, and inside Git Bash. It handles that command fine, but it doesn't solve my problem. I don't know if the second line of the error applies to me, but even if it did, I don't know how to change the command being called because it is done automatically by VSCode. This error occurs every time I launch a terminal in VSCode (even if I don't have any python files present in my workspace), and it happens both when I launch VSCode from the launch button in Anaconda Navigator and when I launch VSCode by itself.

Upvotes: 24

Views: 24561

Answers (10)

mohamednegm
mohamednegm

Reputation: 83

This worked for me go to setting and search for
Terminal.Integrated.DefaultProfile choosing CMD in your OS profile

UPDATE There is also
Terminal.Integrated.Shellargs.Windows
and choose your preferred shell choosing CMD

Upvotes: 5

jimh
jimh

Reputation: 1946

When I changed my default terminal in VS Code to cmd.exe I got conda to work properly for me.

Upvotes: 2

EnthusiastiC
EnthusiastiC

Reputation: 155

This answer is dedicated to Windows 10/11 users, based on PowerShell VS Code integrated terminal, and assumes using miniconda but the same holds for conda under the previous environment.

From the start menu type or search for Anaconda Powershell Prompt then right-click to open its file location. You should see the PowerShell shortcut. Right-click and open properties. In the target bar try to find C:\Users\username\miniconda3\shell\condabin\conda-hook.ps1' ; PowerShell script (.ps1). Using file explorer locate C:\Users\username\miniconda3\shell\condabin. Copy the content of that script.

In the default Documents folder create a folder named WindowsPowerShell if not exists and edit or create a new Microsoft.VSCode_profile.ps1 script in it. Lastly, paste by appending the content of the former script.

You could do the same for the external PowerShell but the script under the path C:\Users\username\Documents\WindowsPowerShell should be named Microsoft.PowerShell_profile.ps1.

In other words, the PowerShell profile script enables the creation of aliases and defining functions that load every time you launch the shell. However, the changes take effect after restarting VS Code and/or PowerShell.

Upvotes: 0

KimO
KimO

Reputation: 131

None of these worked for me. In the end I changed my default VScode terminal to cmd instead of Powershell in the terminal default settings which was None for Windows. I had already added conda.exe to my path and changed the permissions as described above. For some reason the activate.bat file was now found in the path which activated my virtual env.

Upvotes: 1

汪释君
汪释君

Reputation: 21

Since conda activate command would cause CommandNotFoundError, use source activate command instead. It works the same.

I had the same issue. I got the following error:

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'.

I solved it by manully run

source activate base

and Ta-da~ DONE!

This trick is simple, while needed every time you open a new git-bash terminal in VSCode.

Upvotes: 2

Ali Asad
Ali Asad

Reputation: 261

I had the same issue, I've fixed it by adding the Python.CondaPath in settings. Press Ctrl + Shift + P and select Terminal Configuration. Search for python.conda, and paste your conda path for example. C:\ProgramData\Anaconda3\Scripts\conda.exe

This will fix your issue.

enter image description here

Upvotes: 13

Justin D. Harris
Justin D. Harris

Reputation: 2285

In VS code settings, search for "terminal.integrated.shellArgs.windows", then click "Edit in settings.json". For me, this opened "%APPDATA%\Code\User\settings.json". I set "terminal.integrated.shellArgs.windows": "-i -l" and this fixed it for me. My file:

{
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": "-i -l"
}

Upvotes: 2

carl
carl

Reputation: 389

I had the same issue. For me, easily resolved by launching VSC from the conda window.

Specifically, open your cmd prompt (for me, Anaconda Prompt), activate the environment using 'conda activate [envname]'. Then just run the command 'code'. This will launch VS Code with the activated environment and associated variables. From there, the debug works as expected.

Upvotes: 14

mantrasuser3
mantrasuser3

Reputation: 99

I solved this issue by using Powershell. Start the Powershell as Administrator and then type

set-ExecutionPolicy RemoteSigned

Say yes if it asks a confirmation. Now, VSCode debugger option can be used with Python.

Upvotes: 2

Armando Lara
Armando Lara

Reputation: 11

I had exactly the same error as you. I solved it with a tip from a Python course in Udacity

Open Git Bash command line (NOT within VSC terminal) and go to your home folder, e.g., /c/Users/arman. Then run the following two commands but replace [YOUR_PATH] with your Anaconda installation folder

echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc

For example in my case, as I have miniconda, I executed:

echo 'export PATH="$PATH:/c/Users/arman/Miniconda3:/c/Users/arman/Miniconda3/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc

After this executing those lines, i.e., creating the .bashrc file, then run:

source .bashrc

Afterwards, open VSC and try running or debugging a python program. It worked for me!

Upvotes: 1

Related Questions