switch between different conda environments in bash - bash

In the part of my bash script I need to switch to specific conda environment to use package that had been installed specifictly in this environment:
#!/bin/bash
home="$PWD"
# activate VINA 1.2.3
conda activate vina
vina --some_in --some_out
# go back to default environment
conda deactivate
While this works fine simply in the terminal (I am using MacOSX) the sh script executing the same commands produces the following errors:
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
I've tried to use
conda init bash && conda deactivate
which works fine in the terminal but did not solve the problem in the sh script
How I could properly switch between different conda environemnts in the SH script ?

Related

'Conda activate base' fails to load when executed from Matlab on Ubuntu (18.04). Error: conda: command not found

My Conda working perfectly from the ubuntu terminal. I can easily activate the environment I need using the following command
$ conda activate base
and it loads the base environment on the terminal.
But here, in MATLAB, when I try to activate conda environment using the following command it returns an error
>> system('conda activate base')
/bin/bash: conda: command not found
Based on other posts with similar discussions, I have tried the following but to no luck
>> system('/home/anirudha/anaconda3/bin/conda activate base')
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'.
>> system('conda init bash');
/bin/bash: conda: command not found
I have also tried solutions from this but to no success again.
My purpose is to execute a few gdal functions from the command line through a MATLAB script. I have tried doing this on my windows PC, and everything works perfectly. But not on my Linux station. Am I missing something?

anaconda: could not use shell script to switch environment

I have anaconda installed. I am using centOS 8.2, anaconda installation package name is: Anaconda3-2020.11-Linux-x86_64.sh ,
With the initialization, the environment is base. as it shows in the following command line:
(base) [sjj#localhost ~]$
I wrote a shell script named as env.sh as follows, the purpose it to switch environment:
#!/bin/sh
conda activate ray-tutorial
It always say:
(base) [sjj#localhost ~]$ sh env.sh
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'.
But if I directly type the command to switch the environment, it works: My question is why shell script does not work in this case?
(base) [sjj#localhost ~]$ conda activate ray-tutorial
(ray-tutorial) [sjj#localhost ~]$

Activate conda environment within a new xterm terminal

If I run
xterm -hold
and within the new terminal I type
conda activate my_environment
the conda environment "my_environment" is indeed activated.
However, when passing this command using the -e flag, it does not work:
xterm -hold -e "conda activate my_environment"
It instead returns the following error message:
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'.
So, how can this be done using xterm? Or should I use another type of external terminal?
Background
The conda activate command is a shell function that gets defined during initialization of the shell. conda init adds code to the initialization file (e.g., .bash_profile) to run the scripts that define the conda activate shell function.
Solutions
Possible fix: xterm options
When using the -c argument with xterm it no longer runs the initialization script. Hence, conda activate never gets defined. For bash there is the -l that tells it to run the init files. I expected xterm's -ls argument to trigger similar behavior, but it didn't work for me. Perhaps someone more familiar can point you to the correct flag.
Manually Run Conda Script
Otherwise, you just run the Conda script yourself (assuming it's the bash version). Either of these will work:
xterm -hold -e ". /path/to/miniconda3/etc/profile.d/conda.sh && conda activate my_environment && which python"
or
xterm -hold -e "$(conda shell.bash hook) && conda activate my_environment && which python"
The which python is only included to show that you're getting the env activated.
Conda Run
Another option is conda run, which automates executing commands under an environment. The follow is equivalent to what I did in the last section, but without having to know what shell I am running in:
xterm -hold -e "conda run -n my_environment which python"
Just be aware that this functionality is still a work-in-progress. Personally, I've found it quite useful for running simple scripts in specific envs and haven't run into problems.

Failing in Install Anaconda in C-shell

My shell is C shell, I cannot initial and activate conda environment, The following is what happens when I run conda activate:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
In your shell, execute the command:
conda init bash
I am assuming your shell name is bash. If not use it as per your shell.
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell

conda activate on Travis CI

I am using conda 4.6.8 to test a python package in a conda env on Travis CI. I want to replace my old source activate ENVNAME line with the new conda activate ENVNAME command in my Travis CI configuration. If I run this on Travis:
>>> conda update -n base conda
>>> conda init
no change /home/travis/miniconda/condabin/conda
no change /home/travis/miniconda/bin/conda
no change /home/travis/miniconda/bin/conda-env
no change /home/travis/miniconda/bin/activate
no change /home/travis/miniconda/bin/deactivate
no change /home/travis/miniconda/etc/profile.d/conda.sh
no change /home/travis/miniconda/etc/fish/conf.d/conda.fish
no change /home/travis/miniconda/shell/condabin/Conda.psm1
no change /home/travis/miniconda/shell/condabin/conda-hook.ps1
no change /home/travis/miniconda/lib/python3.7/site-packages/xonsh/conda.xsh
no change /home/travis/miniconda/etc/profile.d/conda.csh
modified /home/travis/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
How can I "close and re-open" my shell on Travis? Because otherwise I cannot activate my conda environment:
>>> conda create -n TEST package_names
>>> conda activate TEST
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'.
The command "conda activate TEST" failed and exited with 1 during .
Your build has been stopped.
Not sure it is currently supported as the official doc still uses source in travis.yml.
What does conda init do?
This new command should harmonize the way users setup their shells to be able to call conda activate.
Actually, if you run conda init --dry-run --verbose you will see that it tries to source conda.sh from your ~/.bashrc (assuming you're running Bash, from info mentioned in your question).
And conda.sh will define a conda() function that will catch a few commands among which activate and deactivate and dispatch to $CONDA_EXE:
conda() {
if [ "$#" -lt 1 ]; then
"$CONDA_EXE"
else
\local cmd="$1"
shift
case "$cmd" in
activate|deactivate)
__conda_activate "$cmd" "$#"
;;
install|update|upgrade|remove|uninstall)
"$CONDA_EXE" "$cmd" "$#" && __conda_reactivate
;;
*) "$CONDA_EXE" "$cmd" "$#" ;;
esac
fi
}
So unless this function is defined in your local shell, you won't be able to call conda activate.
Hint on a solution? (not tested for Travis CI)
The only hint I can suggest is to try source $(conda info --root)/etc/profile.d/conda.sh and then conda activate. This should do roughly the same as conda init assuming you are using Bourne shell derivatives.
For csh there is $(conda info --root)/etc/profile.d/conda.csh, and for fish there is $(conda info --root)/etc/fish/conf.d/conda.fish
Note: although not tested for Travis CI, this solution works for me from bash. Of course, the conda executable should be found in PATH for conda info --root to work properly.

Resources