85

Does anyone know how to automatically set environment variables when activating an env in conda? I have tried editing */bin/activate, but that adds the new environment variables for every new env that is created. I want to set env variables that are specific to each env.

PythonRunner
  • 1,531
  • 2
  • 12
  • 11
  • 3
    There is a duplicate of this question here which provides some additional detail people may find useful in the future: https://stackoverflow.com/questions/46826497/conda-set-ld-library-path-for-env-only – David Parks Mar 27 '18 at 16:23

4 Answers4

84

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.

taper
  • 528
  • 5
  • 22
asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • 8
    Thanks! I created the folder $PREFIX/etc/conda/activate.d, put env_vars.sh inside it and specified a some env variables in env_vars.sh. The specified env vars loaded on activation. – PythonRunner Jul 27 '15 at 16:26
  • 8
    Is there a similar solution for Windows? – Paul Apr 04 '16 at 09:15
  • 1
    @PythonRunner Thanks, that was it! See [here](http://conda.pydata.org/docs/using/envs.html#saved-environment-variables) for these instructions. – Greg Sadetsky May 23 '16 at 19:18
  • 4
    for **windows**: check this [link](https://conda.io/docs/user-guide/tasks/manage-environments.html#windows) – gsuresh92 Dec 17 '17 at 19:55
  • 2
    how do you find "$PREFIX"? I tried to figure it out by looking at `which python` within the env, but that folder tree has no `etc/` – Dima Lituiev Mar 21 '18 at 17:46
  • @DimaLituiev you may need to create the `etc/conda` directory. – asmeurer Mar 24 '18 at 03:50
  • 1
    I have the same question as @DimaLituiev, I have a directory `anaconda3/etc/` but not sure if I should create `conda` under that directory, and if so, how I would specify the environment I want to edit `activate.d` for. – David Parks Mar 27 '18 at 16:11
  • For posterity, this answer covers the details related to `$PREFIX` very well: https://stackoverflow.com/questions/46826497/conda-set-ld-library-path-for-env-only – David Parks Mar 27 '18 at 16:21
  • 1
    If you're using PowerShell on Windows, you just need to create `env_vars.ps1` files instead of `env_vars.bat` files, and, within these, set/unset the environment variables accordingly (this is currently omitted from the [documentation](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#windows). – Adam Feb 05 '19 at 15:00
  • Is this still valid with the current `conda actvate` instead of `source activate`? – O.rka Aug 12 '19 at 18:10
  • Since a couple of days (conda 4.9) it's possible to define them in your environment.yml-file. You can find instructions in same link as given in answer. – user511 Nov 16 '20 at 20:18
81

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.

merv
  • 67,214
  • 13
  • 180
  • 245
  • 5
    This is the most up-to-date answer; the accepted answer should be this one. I personally think the YAML specification is the best way to go, as you can commit it to version control. – Anatoly Makarevich Oct 20 '21 at 17:24
  • 2
    The [documentation](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#setting-environment-variables) has more details on setting environment variables in this manner. – jskattt797 Mar 08 '22 at 23:49
  • 1
    If the variable that I am defining is PATH, for example, how can I append to it instead of just overwriting it? – scottlittle Sep 12 '22 at 03:44
  • 1
    @scottlittle unsure, but good question - post it is a separate question – merv Sep 14 '22 at 05:37
  • 1
    [Here's](https://stackoverflow.com/questions/73721599/in-the-conda-environment-yml-file-how-do-i-append-to-existing-variables-without) the question related to my previous comment. – scottlittle Sep 15 '22 at 04:00
13

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).

Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
2

So for virtualenv on Ubuntu I did the below where my virtual environement names is my_env and my environmental variables I want to persist were VAR_A and VAR_B:

virtualenv my_env
vim my_env/bin/activate

This opens the file and you can append your env variables to the end of the file like the below:

# This is me env variables to persist
export VAR_A=/home/developer/my_workspace/var_a
export VAR_B=/home/developer/my_workspace/var_b

Then exit the file.

Activate your virtualenv with

source my_env/bin/activate

Then your env variables should be good. Can verify like the below:

printenv | grep VAR_
VAR_B=/home/developer/my_workspace/var_b
VAR_A=/home/developer/my_workspace/var_a
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jnmcclai
  • 59
  • 4