How to set specific environment variables when activating conda environment?

The accepted answer (conda/activate.d and conda/deactivate.d) works well enough, but it is inconvenient if you want the environment variables to be version controlled without putting the entire environment into version control too. Generally you'd want to store only the environment.yml file in version control.

(I understand that this does not apply to all projects - sometimes the entire reason for using environment variables is to prevent that particular configuration getting stored in version control.)

My preference (on Windows, but the same principle would apply on Linux) is to create a (version-controlled) activate.cmd file in the root of the project directory that sets the environemnt variable(s) and then calls conda's own activate.bat script.

Example (a per-project pylint configuration):

set PYLINTRC=%cd%\pylintrc
@activate.bat %cd%\env

Note that on Windows at least you have to set the environment variables before calling activate.bat because the call to activate.bat never returns to the calling batch file. You also have to name your own script something other than activate.bat to avoid recursion, which is why I chose the cmd extension (which is treated by Windows as a batch file in this context).


Use the files $CONDA_PREFIX/etc/conda/activate.d and $CONDA_PREFIX/etc/conda/deactivate.d, where $CONDA_PREFIX is the path to the environment.

See the section on managing environments in the official documentation for reference.


Environment Variables as Configuration Settings

Conda v4.8 introduced a new command-line interface in the conda-env tool for managing environment variables on a per-environment basis. The command is conda env config vars and here is the help description as of v4.8.3 for the command overall:

$ conda env config vars -h
usage: conda-env config vars [-h] {list,set,unset} ...

Interact with environment variables associated with Conda environments

Options:

positional arguments:
  {list,set,unset}
    list            List environment variables for a conda environment
    set             Set environment variables for a conda environment
    unset           Unset environment variables for a conda environment

optional arguments:
  -h, --help        Show this help message and exit.

examples:
    conda env config vars list -n my_env
    conda env config vars set MY_VAR=something OTHER_THING=ohhhhya
    conda env config vars unset MY_VAR

Perhaps a bit verbose, but it avoids having to manually manage files in etc/conda/(de|)activate.d.

YAML Specification

Added in Conda v4.9, there is now support for automatic defining of environment-specific variables as part of an environment YAML definition. For example,

name: foo
channels:
  - defaults
dependencies:
  - python
variables:
  MY_VAR: something
  OTHER_VAR: ohhhhya

which would set up the environment variables MY_VAR and OTHER_VAR to be set and unset on environment activation and deactivation, respectively.

Tags:

Anaconda

Conda