pythonanacondaconda

In the conda environment.yml file, how do I append to existing variables without overriding them?


This is a follow up question for this answer. For a conda environment specification file environment.yml, if the variable that I am defining is PATH, for example, how can I prepend or append to it instead of just overwriting it? Is the following correct?

name: foo
channels:
  - defaults
dependencies:
  - python
variables:
  MY_VAR: something
  OTHER_VAR: ohhhhya
  PATH: /some/path:$PATH

Solution

  • It dependes on whether you are using windows or linux. A look at the source code of the environment init code reveals that conda itself simply executes bash (linux) or cmd.exe (win) calls:

    linux:

    yield from (f"export {envvar}='{value}'" for envvar, value in sorted(env_vars.items()))
    

    windows:

    yield from (f'@SET "{envvar}={value}"' for envvar, value in sorted(env_vars.items()))
    

    So make sure that you are using the correct syntax for your variable. In case of linux, that would be

    variables:
      MY_VAR: something
      OTHER_VAR: ohhhhya
      PATH: /some/path $PATH
    

    AFAIK windows uses ; to delimit entries, so you would probably have to do this (untested):

    variables:
      MY_VAR: something
      OTHER_VAR: ohhhhya
      PATH: /some/path;%PATH%