5

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arafat Khan
  • 777
  • 1
  • 10
  • 24

3 Answers3

14

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:

  1. Open Git Bash and go to your home directory with: cd. Just type this and you will be in your home directory
  2. 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
  3. 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

Arafat Khan
  • 777
  • 1
  • 10
  • 24
  • I've found using the `bash -l` command in the git bash terminal seems to accomplish what you wanted to do, i.e. VS Code will refer to the `.bash_profile` file now after the `conda init` step (run once) without needing to create or edit any config files. This seems to do what you intended I think. I've put this in my answer below. – decoder247 Jun 07 '20 at 11:36
  • Good solution! This works fine too. But it is not actually run once. Every time you wish to activate environments using Git Bash in VS Code terminal, you need to run `bash -l`. You can put this `bash -l` in your `.bashrc` so that it is automatically called. – Arafat Khan Jun 07 '20 at 11:54
  • This is great! I just had to copy my ~/.bash_profile to ~/.bashrc and `source ~/.bashrc` and then everything worked great :) – John Targaryen Nov 21 '20 at 01:23
3

EDIT: A better solution than using source activate to get conda activate commands to work in the git bash terminal in VS Code:

  1. Run conda init in the Git Bash Terminal in VS Code
  2. Type in bash -l in VS Code's Git Bash terminal to launch your configured shell as a login shell
  3. 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:

  1. source activate env-name - You should see your line appended by the (base) tag now.

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

decoder247
  • 134
  • 2
  • 12
  • 1
    Hey there. After reading your answer, I researched a bit more and updated my answer that I believe could be a decently complete solution to this problem. I have added more information and also included helpful code to help others solve this quickly. Also, I have included information regarding your usage of `source activate`. Please read my updated answer to know more about why you should set up conda properly (and how to do it) and why you should only use `conda activate`. – Arafat Khan Jun 07 '20 at 08:02
  • Thanks Arafat, changed my answer and learned something new about the `source activate` command! Probably a newb question - but I'm still a bit unclear about where you can find the correct `.bashrc` and `.bash_profile` to ammend in your answer? Maybe you can provide the filepaths for those and what to do if you can't locate them? Searching windows I found a `bash.bashrc` file in my git installation's etc folder and several `.bashrc` files in my WSL installation folders but I'm not sure if these are the correct files – decoder247 Jun 07 '20 at 08:24
  • Thank you for bringing to my attention that locating those two files could be a bit of a problem for some. I have updated my answer providing clear details on how to locate them or create them if they are already not present. Please check it out. – Arafat Khan Jun 07 '20 at 08:51
0

A year later I am still running into this issue. The following is a streamlined and updated approach based on Arafat's answer.

  1. Install Git Bash

  2. Configure Git Bash to be used in VSC (see How do I use Bash on Windows from the Visual Studio Code integrated terminal?)

  3. Open the git bash Terminal from VSC

  4. If conda activate is run successfully, skip the rest

  5. run

    conda init bash

  6. Check for the exiting bash dot files:

    ls -al ~/.bash*

  7. Likely only one of '.bashrc' and '.bash_profile' exist

  8. 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.)

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

  2. Reopen the Terminal in VSC

robert
  • 978
  • 7
  • 17