4

When I do conda info --envs, I get list of all envs and their locations like this:

# conda environments:
#
base                  *  /Users/mbp/miniconda3
myenv              /Users/mbp/miniconda3/envs/myenv

Is there a way to get the location of myenv environment from command line? May be something like conda info --envs myenv to get

/Users/mbp/miniconda3/envs/myenv

What's the use case?
I want to cache all the environment dependencies in GitHub actions. This has to happen before the environment is activated. If I know the location of the environment, I can cache all the files in it.

Ruchit
  • 336
  • 3
  • 16

3 Answers3

3
conda info --envs | grep -Po 'myenv\K.*' | sed 's: ::g'

This bash command will retrieve all envs from conda and find the line which starts from myenv and give it to the sed command which inturn removes the spaces

surprisingly it worked for me

Jack
  • 788
  • 3
  • 13
  • Not working. Giving me `BrokenPipeError: [Errno 32] Broken pipe` – Ruchit Sep 13 '21 at 06:54
  • Check the docs https://docs.python.org/3/library/exceptions.html#BrokenPipeError it's a connection error somewhere in the code. this command is not responsible for that – Jack Sep 13 '21 at 06:56
3
$(conda info --base)/envs/myenv
hermidalc
  • 498
  • 4
  • 12
1

Conda's internal function to handle prefix resolution (locate_prefix_by_name) is currently located in conda.base.context, so one could avoid listing all envs with a script like:

conda-locate.py

#!/usr/bin/env conda run -n base python

import sys
from conda.base.context import locate_prefix_by_name

print(locate_prexif_by_name(sys.argv[1]), end='')

Usage

# make executable
chmod +x conda-locate.py

./conda-locate.py jupyter
# /Users/user/miniforge/envs/jupyter

You may want to add a try..catch to handle the conda.exceptions.EnvironmentNameNotFound exception.

merv
  • 67,214
  • 13
  • 180
  • 245