0
votes

When I activate a Conda environment in a Bash script like:

#!/bin/bash
conda activate ./myenv
which python
python do_stuff.py

The activate line prints out the long warning:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - 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'.

How do I silence this warning? If I run the conda init bash command it suggests, that causes Conda to activate my environment by default for all Bash shells, which is definitely not what I want. That's the default configuration in Conda and I explicitly turned that off after it broke functionality everywhere across my system. I want Conda to function similar to virtualenv, where I have a folder containing the environment, and activate it only when needed for a specific application. Under no circumstances do I want a Conda environment activated for every instance of Bash run anywhere on my system.

I'm not sure why it's giving me a CommandNotFoundError, because otherwise Conda populates my Bash shell with the correct paths to Python and other environment-specific resources, and everything else works correctly.

2

2 Answers

2
votes

Prepend the following code in your script.

eval "$(command conda 'shell.bash' 'hook' 2> /dev/null)"

This initialize the conda in the script only.

0
votes

Once you initialize conda it will auto activate the base environment on every new shell session you open, by default. If you would like to turn this behavior off, just run once:

conda config --set auto_activate_base false