4

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?

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • 1
    Where in the example here is `conda activate` called? Also, a lazy option would be to source the initialization file: `conda init bash && source ~/.bashrc` – merv May 06 '22 at 19:12
  • @merv fixt the cut and paste error: the `conda activate` is shown now. This script is intended for use within a python script that might be running just about anywhere. The point is to dissociate from updating the user's home directories – WestCoastProjects May 06 '22 at 19:17
  • Perhaps you might benefit from a proper standalone executable like [`micromamba`](https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html), which includes activation functionality without touching users' resource files. Currently, the OP approach seems hackish to me and likely an XY problem. Consider describing in the question a production-stage vision of what ultimately is trying to be accomplished. – merv May 06 '22 at 19:37
  • @merv I am just completely beyond exhausted with these python environments having unexpected behavior. Days of it. I installed `micromamba` now , did `micromamba install python=3.8 py38_mamba` and get `error libmamba No target prefix specified` . No hits googling. I guess i can make another question on that one.. – WestCoastProjects May 06 '22 at 20:05
  • `Micromamba` documentation is insufficient for me to get an environment up and running – WestCoastProjects May 06 '22 at 20:45

1 Answers1

4

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)"
merv
  • 67,214
  • 13
  • 180
  • 245