0

We can use

conda env config vars set MY_VAR=something

or in an environment.yml block

variables:
  MY_VAR: something
  MY_VAR2: ${CONDA_PREFIX}/abc

to define variables for the conda environment.

The set variables become available when we do conda activate <env> and are unset on con deactivate.

See e.g.

I would expect the definitions to be in

<env location>/etc/conda/[de]activate.d

but the are not, even though after conda activate <env> they are shown when I do

conda env config vars list

This gives something like

MY_VAR = something
MY_VAR2 = ${CONDA_PREFIX}/abc

With $CONDA_PREFIX not being expanded. When I do

conda env config vars set MY_VAR2=${CONDA_PREFIX}/abc
conda deactivate 
conda activate <env>

it works as expected and $CONDA_PREFIX is expanded.

So,

  • Where are the variables defined, i.e. set / unset?
  • Why is $CONDA_PREFIX not expanded when used in the environment.yml?
  • How to set variables using environment variables via environment.yml?
Stefan
  • 1,697
  • 15
  • 31

1 Answers1

0

The conda env environment variables use a separate mechanism. It stores variables in a conda-meta/state file. So, for example:

$ conda create -n foo
$ conda activate foo
(foo) $ conda env config vars set MY_VAR=something
(foo) $ cat $CONDA_PREFIX/conda-meta/state
# {"env_vars": {"MY_VAR": "something"}}

This is a crude mechanism that does not support dynamic variables (i.e., using $CONDA_PREFIX). Your example at the CLI, is expanded by the shell prior to it being passed as an argument to Conda. Whereas Conda never passes the YAML through a shell expansion, so it directly copies the literal text to the conda-meta/state file.

If you require dynamic variables, then use activation scripts. The current Conda idiom for delivering reusable activation scripts in an environment is via a Conda package. Some details in Add `activate.sh` and `deactivate.sh` to Conda Environment Created From YAML FIle.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Thanks. Ok, thats a pity. Any idea if there are plans to change this behavior and have both ways create an [de]activation script, with proper shell expansion? A quick search on existing , open issues did not help. – Stefan Aug 23 '23 at 05:09