''conda init' without closing the current shell
There are a number of use cases for which I am trying to use conda . The main headache is that conda init just does not want to play fair within the flow of a script in the same bash shell. I frequently see
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
That happens even though the contents of the conda initialize script as well as conda init have been executed. The results are inconsistent: sometimes I have seen the init logic work, others not. I have not been able to ascertain what magic is happening and what it expects in order to work properly..
conda_init() {
# __conda_setup="$('$CONDA_DIR/bin/conda' 'shell.bash' 'hook' 2> /dev/null)";
source $CONDA_DIR/etc/profile.d/conda.sh
export PATH="$CONDA_DIR/condabin:$CONDA_DIR/bin:$PATH";
export CONDARC=$CONDA_DIR ;
conda init bash
}
conda_init
conda activate py38
That gives us
/Users/steve/opt/miniconda3/bin/conda
/Users/steve/opt/miniconda3/bin::/Users/steve/opt/miniconda3/condabin:<other stuff..>
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 can that conda_init() be made reliable for being able to run conda init in the same shell?
Solution 1:[1]
Generally, one should not need to "activate" an environment when working programmatically. That's what conda run is for...
conda run -n py38 python my_script.py
Otherwise, if CONDA_DIR is defined, then the following would run the initialization shell commands in an active bash session:
eval "$(${CONDA_DIR}/bin/conda shell.bash hook 2> /dev/null)"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | merv |
