How do I find the name of the conda environment in which my code is running?

You want $CONDA_DEFAULT_ENV or $CONDA_PREFIX:

$ source activate my_env
(my_env) $ echo $CONDA_DEFAULT_ENV
my_env

(my_env) $ echo $CONDA_PREFIX
/Users/nhdaly/miniconda3/envs/my_env

$ source deactivate
$ echo $CONDA_DEFAULT_ENV  # (not-defined)

$ echo $CONDA_PREFIX  # (not-defined)

In python:

import os
print(os.environ['CONDA_DEFAULT_ENV'])

for the absolute entire path which is usually more useful:

Python 3.9.0 | packaged by conda-forge | (default, Oct 14 2020, 22:56:29) 
[Clang 10.0.1 ] on darwin
import os; print(os.environ["CONDA_PREFIX"])
/Users/miranda9/.conda/envs/synthesis

The environment variables are not well documented. You can find CONDA_DEFAULT_ENV mentioned here:
https://www.continuum.io/blog/developer/advanced-features-conda-part-1

The only info on CONDA_PREFIX I could find is this Issue:
https://github.com/conda/conda/issues/2764

Leave a Comment