Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific environment variables when activating conda environment?

Tags:

conda

anaconda

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.

like image 907
PythonRunner Avatar asked Jul 23 '15 22:07

PythonRunner


People also ask

How do you specify a new environment location for conda?

In summary, if you edit . condarc to include D:\envs , and then run conda env create -p D:\envs\myenv python=x.x , then activate myenv (or source activate myenv on Linux) should work. Hope that helps!

Can I activate two conda environments at the same time?

After provisioning, the conda activate command needs to be repeated each time you want to run the program in that environment. In your second script example, you're creating two environments from yml files, just to re-export the list of installed packages, and then install them into the base environment.


2 Answers

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.

like image 59
asmeurer Avatar answered Nov 08 '22 21:11

asmeurer


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.

like image 32
merv Avatar answered Nov 08 '22 20:11

merv