VSCode conda activate base giving CommandNotFoundError - python

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.

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.

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.

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"
}

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.

This worked for me go to setting and search for
terminal.integrated.shellargs.windows
then choose Command Prompt like this

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!

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.

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

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.

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.

Related

Is it necessary to run activate command after activating virtual environment using pipenv shell command?

As far as I know, for activating the virtual environment it's enought to just run pipenv shell
But in my case after running this command the parentheses at the beginning of the command prompt doesn't show up. Even after selecting the proper interpreter (the one inside venv folder)
To show the parentheses I should run activate command afterward.
So my question is: Is it important to show the project name in parentheses at the terminal prompt? Should I run the activate command after pipenv shell command every time?
I will appreciate if you describe the meaning of those parentheses. Tnx.
PS: I am using vscode in windows 10.
You do not need to activate the environment manually. When you select the python interpreter and open the terminal, vscode will activate it automatically for you.
The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Terminal command. In the latter case, VS Code automatically activated the selected environment.
You can read document for more details.

How to activate virtual environment in Vscode when running scripts are disabled in system?

I created a virtual environment in vscode in a folder called server by typing:
python -m venv env
And I opened the server folder, select interpreter Python 3.8.1 64-bit('env':venv)
then I got following error:
I can't find any solution to this and I am stuck for hours.
It seems that it is going to activate the environment through a powershell script. And running such scripts is turned off by default. Also, usually a virtual environment is activated through cmd and .bat script. You could either turn on running powershell script or make VS Code activate an environment through cmd and .bat file.
The first way - using cmd instead of Powershell
I just checked it in my PC and VS Code doesn't use Powershell at all. It activate an environment with cmd instead of Powershell. Probably it is worth to check VS Code settings, set cmd as a default terminal. It is probably such an option in the main settings.json (you can open it through ctrl+shift+p and type 'open settings (JSON)'): "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",.
The second way - changing Powershell execution policy
In order to change Powershell execution policy you can add "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] to your main VS Code settings.
Also you can open a Powershell window as administrator and type the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then respond y to any questions.
An update with regards to using the Command Prompt instead of Powershell in VS Code:
When implementing "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe" in settings.json it gives the notice:
This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#. This will currently take priority over the new profiles settings but that will change in the future
After taking a look at the documentation I've found that the correct alternative would be to include a line as shown below.
"terminal.integrated.defaultProfile.windows": "Command Prompt"
Alternatively, you can also use the GUI as shown in the documentation. More advanced settings are also shown there.
Try Using Cmd in vscode and Run this command env(your Virtual Env name)\Scripts\activate

Stuck when setting up to use anaconda with VS Code and Integrated Git terminal

I want to learn Data Science and so have used some really popular Python modules likes Pandas, Matplotlib, Numpy, etc. So I clean installed Anaconda and am now using it as my default Python interpreter and also using Conda for installing packages and making virtual environments. I use VS Code as my daily text editor. But I have run into some issues when using the integrated Git terminal in VS Code with the Anaconda Python interpreter.
There are a couple of issues that I am facing. One of the first issues that I see is when I am using CMD to run Python. If I type and enter python in cmd, the Python interpreter provided by anaconda comes up. But I also get a warning:
Warning:
This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation
I didn't expect to get this output. Anyway, there's another problem in VS code. But first I would like to mention that I have checked "Add to PATH" when installing Anaconda so no issues there. Now, when I open a new Terminal in VS Code, automatically C:/Users/User/Anaconda3/Scripts/activate is run and then conda activate base is run. But when conda activate base is run, automatically, as mentioned, I get a CommandNotFoundError. It states 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'
And then I am told to initialize my shell, so I did conda init bash but still no luck. And this brings me to talk about .bash_profile. I think it has to do something with this bash profile. Anyway, this is what is in my bash profile
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/User/Anaconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<
Just a summary of the problem:
Unexpected warning in CMD when running Anaconda Python interpreter
Automatically run Anaconda Scripts and conda activate base when opening new Terminal in VS Code
Conda init bash not helping
P.S I have tried using conda activate [env_name] in CMD and also in Git Bash and they work without any issues. In other words, Anaconda and Conda work perfectly outside of VS Code terminal.
I have figured out the answer myself and would like to share it here. First of all at the time of writing the question I was using Git Bash as my Terminal in VS Code (am still using it). So the issue was that when I ran the command conda init bash in Git Bash or the VS Code Terminal, Conda just basically put the command used for activating Conda environments in the .bash_profile since it is sourced during logging into Bash. But the integrated Terminal in VS Code is a subshell of a Git Bash session. That is why .bash_profile is NOT sourced in VS Code since .bash_profile is only sourced during the main Bash session. The .bashrc file is the file that is sourced when creating a Terminal session in VS Code. So what you actually need to do is take the code that is put into .bash_profile by conda init bash and paste it into your .bashrc file and make the .bash_profile source that .bashrc file automatically.
So, to sum up, using conda init bash will put the conda command in the .bash_profile and it is usually sourced by Git Bash, but VS Code Git Bash terminal will use .bashrc.
So you can just cut and paste the code from .bash_profile to .bashrc (as already mentioned) or if you want, just simply follow this: put this code in your .bash_profile:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
And in your .bashrc, put this code:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('{path_to_your_conda.exe}' 'shell.bash' 'hook')"
# <<< conda initialize <<<
# You can get conda.exe path by using `which conda` in Git Bash
Now, when you open up a Git Bash session in VS Code Terminal, you can use conda activate env_name to activate any environments you have.
Everything is now supposed to work as expected in VS Code terminal but I would like to further elaborate about something. If you want, you can skip the conda init bash process (NOT recommended, just read on for additional knowledge but please follow the above steps only). This is a feature that was introduced in conda 4.4.0. Till then the way of activating conda environments was by using source activate but that command was NOT cross-platform, meaning that the command could not be used in OSes like Windows.
So they made this change by introducing commands like: conda activate env_name so that conda environments become much easier to use despite the OS platform.
conda activate also has other advantages. This is directly from their release docs:
conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You'll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like Python virtualenv's activate script.
I used this question as a reference. Check it out to learn more.
Having said that, using source activate env_name will still work if you are using Git Bash, even in VS Code Git Bash terminal. source activate env_name requires no prior set up or config. But it is highly recommended that you only use conda init to set everything up and then use conda activate env_name.
[NOTE]: Locating and modifying the said .bashrc and .bash_profile on Windows is usually not as simple as it is on Linux. But can be done fairly easily like this:
It goes without saying but, you should have Git Bash installed. Having Git Bash installed should, as far as I know, automatically create .bashrc or .bash_profile or maybe both. These files are called "dotfiles" (since they start with a dot) and these are by default hidden on most OSes and definitely on Windows. If they were auto-created by Git Bash on your system, it is most likely that they are placed in your home directory. Home directory on Windows is C:\Users\<you>\. With that said, follow this:
Open Git Bash and go to your home directory with: cd. Just type this and you will be in your home directory
Enter this command: ls -a and you will see all your files, even hidden ones. Look for .bash_profile and .bashrc. Both should be present. If they are, you are ready to follow the above instructions. But if one is not there or if both are missing create them using: touch .bashrc && touch .bash_profile. You should now see these files when you again type: ls -a
That's it. Now that you have your .bashrc and .bash_profile, you can follow the above instructions. Also, to access these two files quicker, open them like this with VS Code: code ~/.bashrc or code ~/.bash_profile. Now, modify these two files as per the instructions.
In the question, I have also talked about VS Code activating Conda environments automatically. There's no issue with VS Code doing that since this is the default behavior.
I misinterpreted that as something that's an issue. But if anyone was looking to stop VS Code from automatically doing that, I would recommend trying to set this in the user settings:
"python.terminal.activateEnvironment": false
EDIT: A better solution than using source activate to get conda activate commands to work in the git bash terminal in VS Code:
Run conda init in the Git Bash Terminal in VS Code
Type in bash -l in VS Code's Git Bash terminal to launch your configured shell as a login shell
You should now be able to run conda activate commands per normal!
More info: bash -l runs your ~/.profile/~/.bash_profile/~/.zprofile scripts where the conda executable is actually referenced (but in which Git Bash as a integrated terminal does not run by default and refers to). Hence, git bash does not know where to search for conda when running conda activate commands and per Arafat's explanation above, running conda init changes the git bash PATHs in this .bash_profile file, but is ineffectual as the git bash terminal in VS Code doesn't actually refer to this file! Further info in VS Code's official docs.
Supplementing the explanation of the accepted answer, I've posted a solution that worked for me here that might possibly help others (changing user settings did not solve the issue for me). Link could also point to other working solutions if the below or accepted answer above doesn't work.
NOTE: Please read Arafat's answer before attempting the source activate method below to understand why it's not normally recommended. That said leaving it up as it still solves the problem.
Here's what worked for me using the Git Bash terminal in VS Code on
windows in succinct steps:
source activate env-name - You should see your line appended by the (base) tag now.
After calling on source activate, I've found following conda activate commands to work: i.e. conda activate env2-name
What didn't work for Git Bash (as a VS Code terminal) for me: activate
env-name and conda activate env-name.
A year later I am still running into this issue. The following is a streamlined and updated approach based on Arafat's answer.
Install Git Bash
Configure Git Bash to be used in VSC (see How do I use Bash on Windows from the Visual Studio Code integrated terminal?)
Open the git bash Terminal from VSC
If conda activate is run successfully, skip the rest
run
conda init bash
Check for the exiting bash dot files:
ls -al ~/.bash*
Likely only one of '.bashrc' and '.bash_profile' exist
Check the existing dot file for conda initialization code
e.g.
cat ~/.bash_profile
This included in my case '>>> conda initialize >>> ...' code
(But, and this is the source of the problem, it is not executed when the terminal is opened. To check which of the files is executed simple add 'echo hello-X' to each of them.)
To fix the problem, we must create the missing dot file and make it execute the OTHER previously existing one:
tee -a ~/.bashrc << END
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
END
Reopen the Terminal in VSC

Anaconda and Git Bash in Windows - conda: command not found

I've installed Anaconda and set Path environment variable to C:\Anaconda3; C:\Anaconda3\Scripts.
Then I try to run in Git Bash
conda install python
But there is an error message "bash: conda: command not found". I would like to know why.
To be able to run conda on gitbash you need to add it to the path. Many times I've seen that's done by default - as shown in the setup for this workshop. If it doesn't, as it seems your case, then you can run their setup directly by running:
. /c/Anaconda3/etc/profile.d/conda.sh
After running that you should be able to run conda commands.
To keep this setup permanently you can add such line on your .profile or .bashrc file (read more about their differences). A way of doing so is running the follwing:
echo ". /c/Anaconda3/etc/profile.d/conda.sh" >> ~/.profile
You may encounter problems if the path where Anaconda was installed contains spaces (e.g., C:\Program Files). In that case you would need to change the anaconda location or edit conda.sh script with something like:
sed -e '/^_CONDA_EXE=.*/a alias myconda="${_CONDA_EXE/ /\\\\ }"' \
-e 's/\$_CONDA_EXE/myconda/g' /c/Program\ Files/Anaconda3/etc/profile.d/conda.sh > conda_start.sh
This sed command inserts a new alias definition myconda which changes the anaconda path from Program Files to Program\ Files so bash doesn't stop with an error like this one:
bash: /c/Program: No such file or directory
The second sed command replaces the _CONDA_EXE variable by the new alias created.
Since the above doesn't modify the file provided by anaconda, you will need to update your .profile file to load the file we've just created, conda_start.sh, instead.
First, you need to move to the directory where conda is located.
(some path such as C/Anaconda3/Scripts or ../miniconda3/Scripts or anaconda3/bin)
then, open the terminal.
(or, if you use Windows and can't find where the conda is, try moving to directory such as C:\Users\User_Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit) and open the Anaconda prompt.)
Then, do this:
conda init
or something like
conda init bash
if you use Mac OS:
conda init zsh
will work well.
if you wanna use different shell,
conda init [shell_name]
[shell_name] can be: bash, cmd.exe, fish, powershell, tcsh, xonsh, zsh, etc.
use conda init --help for more info.
Joining #dvdgc13. In my case, I fixed the problem by adding
. C:/Users/user/Anaconda3/etc/profile.d/conda.sh
to my .bash_profile.
I tried to do the same thing as you did but I couldn't get it to work. starriet had the working answer but I am going to make it easier for everyone else reading. You can directly open command windows with explorer instead of struggling with paths.
Find your Anaconda3 folder with Windows Explorer This could be a user install where would be in your user folder such as "C:/Users/your_name/Anaconda3".
Shift + Right Click on the explorer and click on "Open PowerShell Windows Here". Note: you can just click "Git Bash" and you open Bash instead which makes no difference to the command.
Type in "conda init" in the PowerShell window. This works even if you don't have the right paths because the command line looks for the right exe in the current dictionary. If you scroll down in the explorer, you should be able to find it.
My PowerShell window would look a little different than yours because of my prompt but it makes no difference.
Exit the PowerShell and open Git Bash. Type "conda" to confirm that things work.
For MAC users, do this:
$ echo ". /usr/local/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
$ source ~/.bashrc
I tried many ways, but they are incomplete until I run the following commands:
Go to the path of anaconda3 is C:\Users\USER_NAME\anaconda3 and open commend line over there and print the following: (YOUR_PATH = C:\Users\USER_NAME\anaconda3 )
echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >> .bashrc
echo 'alias python="winpty python.exe"' >> .bashrc
If Git bash is opened, close it and reopen it again and type the following to make sure anaconda and python work without problems:
conda --version
python -- version
If you see the versions are printed, everything works well.
In my case conda command was recognised in cmd, but not in bash. I used conda init bash instead of simple conda init in cmd.
This command modified the .bash_profile file, which was residing in my C:\Users\xyz directory, and added the following code in it
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/Saifullah/miniconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<
now it is working in bash terminal too.

Where is the .profile file on mac?

I have a MacBook Pro 13` inch (without touch bar) and it is running mac os sierra. I have installed Xcode, command line tools and homebrew. Now I want to install python 3 and make it a default interpreter when calling it from the terminal. I have found this guide http://docs.python-guide.org/en/latest/starting/install3/osx/ and I get confused when they say I should add the path to the ~./profile but I don't know where to find the file. Can somebody help with step by step guide on how to create it if it not there?
Thanks
I felt to add something here that ate a lot of my time when I had a similar issue.
Many a times the .profile or the .bash_profile (in case of bash shell) is not present. This is typically when you have freshly installed the OS or when you're on a new machine. This gets confusing as one may think if the installation wasn't correct etc. Nothing to worry about. If you don't find these files then simply create it new using vi or whatever means (GUI) and you're good to go. Having a shell doesn't mean you have these files too and you have to create it yourself if you're the first one to use it.
The file you are looking for depends on your shell, so if you are using bash, which is the default shell on Mac systems, it will be either ~/.bash_profile, or ~/.bashrc depending on whether you are in a login shell or non-login shell respectively.
Mac Terminal runs a login shell every time a new terminal window is open, and it calls .bash_profile, so you should use ~/.bash_profile.
On Mac, zsh is the default shell. You can check this by running
echo $SHELL
If so, you can check for ~/.zshrc if not you can create one and add required commands, alias which need to run during the terminal startup.
Then you can run
source ~/.zshrc
This will be executed every time the terminal is opened.
It could be that you are not running bash (rather an extended version like zsh, ksh, etc)
If you are running bash shell, then you should be able to see ~/.bash_profile, or ~/.bashrc or ~/.profile.
Incase of zsh shell, you would see .zprofile or .zshrc
You can check what shell you have by echo $SHELL
After you install "Brew", there is a prompt in the terminal that says: "Next Steps:"
Follow those steps!

Categories