Unable to activate environment - prompted to Run 'conda init' before 'conda activate' but it doesn't work

Running Bash in Cmder on Windows 10. I am trying to activate a new virtual environment but keep getting told to run ‘conda init’ before ‘conda activate’. I’ve tried running conda init but I keep getting prompted to do it again.

I’m guessing that it might have something to do with how I installed miniconda. I followed the instructions here which specifically states:

“On Windows, you’ll be asked if you want to add Miniconda to your PATH variable. Although it recommends that you do not do this, DO add it to your PATH. This will be important when we change how our command line works.”

But when I read up on the conda documentation here it states:

“When installing Anaconda, you have the option to “Add Anaconda to my PATH environment variable.” This is not recommended because it appends Anaconda to PATH. When the installer appends to PATH, it does not call the activation scripts.”

I’m guessing that this is the issue but I don’t know of a work around (if one is needed).

Actions and Errors below:

I create my environment:

conda create --name test_env

The environment is created and then I am told to:

# To activate this environment, use
#
#     $ conda activate test_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

I attempt to activate:

conda activate test_env

Ouput

CondaError: Run 'conda init' before 'conda activate'

I run ‘conda init’:

λ conda init
no change     C:\Users\Username\miniconda3\Scripts\conda.exe
no change     C:\Users\Username\miniconda3\Scripts\conda-env.exe
no change     C:\Users\Username\miniconda3\Scripts\conda-script.py
no change     C:\Users\Username\miniconda3\Scripts\conda-env-script.py
no change     C:\Users\Username\miniconda3\condabin\conda.bat
no change     C:\Users\Username\miniconda3\Library\bin\conda.bat
no change     C:\Users\Username\miniconda3\condabin\_conda_activate.bat
no change     C:\Users\Username\miniconda3\condabin\rename_tmp.bat
no change     C:\Users\Username\miniconda3\condabin\conda_auto_activate.bat
no change     C:\Users\Username\miniconda3\condabin\conda_hook.bat
no change     C:\Users\Username\miniconda3\Scripts\activate.bat
no change     C:\Users\Username\miniconda3\condabin\activate.bat
no change     C:\Users\Username\miniconda3\condabin\deactivate.bat
modified      C:\Users\Username\miniconda3\Scripts\activate
modified      C:\Users\Username\miniconda3\Scripts\deactivate
modified      C:\Users\Username\miniconda3\etc\profile.d\conda.sh
modified      C:\Users\Username\miniconda3\etc\fish\conf.d\conda.fish
no change     C:\Users\Username\miniconda3\shell\condabin\Conda.psm1
modified      C:\Users\Username\miniconda3\shell\condabin\conda-hook.ps1
no change     C:\Users\Username\miniconda3\Lib\site-packages\xontrib\conda.xsh
modified      C:\Users\Username\miniconda3\etc\profile.d\conda.csh
no change     C:\Users\Username\Documents\WindowsPowerShell\profile.ps1
modified      HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

==> For changes to take effect, close and re-open your current shell. <==

I exit out of the shell, reopen and run:

conda activate test_env

And again get

CondaError: Run 'conda init' before 'conda activate'

I have tried executing these steps in various orders and closing the shell vs not but I am stuck in the same loop.

How can I get activate my environments if miniconda is installed to the User PATH?

2 Likes

Please restart your shell and open it again in your shell, then your conda will be activated.

conda init needs shell name sometimes

if using zsh,

run:

conda init zsh

exit out of the shell, reopen and run:

conda activate test_env

2 Likes

Try this,
source activate base
and then,
conda activate my_env
It works !!!

3 Likes

This worked for me. Using oh-my-zsh and hyper on macOS.

I have the same issue. Have you already solved it?

ajaypunna9342 You are a genius. It worked. I am copy and pasting that to a doc all by itself and leaving on my desktop!

Well you can run this command in shell “Set-ExecutionPolicy RemoteSigned”
it will change the execution policy to RemoteSigned.
Hope this works for you

1 Like

This worked for me on Windows 11 VS Code, using mingw64 bash.

I spent some time digging into this and I think I understand why this happens - or maybe one of the reasons this happens. The first thing I had to understand is that the conda init code that generally gets automatically added to your ~/.bashrc and looks something like this:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/path-to-conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/path-to-conda/etc/profile.d/conda.sh" ]; then
        . "/path-to-conda/etc/profile.d/conda.sh"
    else
        export PATH="/path-to-conda/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Has to be run in every shell whether it’s a login shell or a sub-shell (say an xterm launched by your window manager). That means:

  • The “conda init” code has to be in your SHELL’s init file (e.g. ~/.bashrc, ~/.cshrc, etc) and not in a login init file (e.g. ~/.profile, ~/.bash_profile, etc). Those login init files only get executed once when you first log in (unless you explicitly set the -l flag which you shouldn’t generally do).
  • The “conda init” code has to be executed no matter how the shell was invoked. What I mean is that the init code should occur before any statement like the following snippet below in your SHELL’s init file. Note that the following code is in every default Ubuntu ~/.bashrc I’ve seen recently and causes the rest of the init file to be ignored if the shell is not interactive (for example in any bash script).
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
  • Whenever you run conda init, the conda init code is automatically added to the end of your SHELL’s init file which means it won’t get executed by a default Ubuntu ~/.bashrc file if the shell is not interactive.

All this causes problems if you are writing scripts that require a specific environment. For example, if myscript.sh looks like this:

#! /bin/bash -e

# First I need to make sure I'm using the right environment:
conda activate task-specific-env

python /some-path/task-specific-script.py

It will fail if the conda init code is not executed for non-interactive shells.

One more comment on this: if you are having this issue with SLURM sbatch, please note that the sbatch mechanism does not invoke your shell when it runs your script. That means the conda init code will never be run! One way to fix this is to put the conda init code in some file like ~/.conda_init and source that file in your sbatch script right after the “shebang”, for example:

#! /bin/sh

. ~/.conda_init
conda activate my-env

...

According to @ajaypunna9342 , you would need to do “source activate base” and then “conda activate my_env”

I encountered the same issue as you did, and all i needed to do is:
“source activate <-env-name->”

Hence, in your case it would be “source activate test_env” and you do not need to do a subsequent “conda activate test_env”

I think this is true, as when i inspected the python interpreters, activating ‘base’ environment will point to the default python interpreter installed by miniconda. Hence, in your case, all you needed is activate the ‘test_env’ environment.

image

Correct me if im wrong yalls