3

I submitted an issue on an open source python library and received feedback that the devs couldn't reproduce the error. I had installed the package into a conda environment, and I want to figure out what environment(s) I installed the package into so I can try to reproduce the issue in its original environment. The problem is that I have several conda envs to poke through, and my current strategy of "activate an environment -> start python interpreter -> try to import the package -> exit interpreter -> deactivate environment" is getting old.

Is there a simple way to list all environments which contain a certain package? Something like:

conda info --envs --package=PackageName

EDIT: I've figured out how to check if a package is installed in any of my environments. Still doesn't alert me to which environment has the package, just shows me a hit if the package exists:

Continuum/anaconda3/condabin/conda.bat info --envs | awk '{print $1}' | xargs -ix Continuum/anaconda3/condabin/conda.bat list -n x | grep packagename

This is on a windows machine, using a git bash shell, with the working directory set to /c/Users/userName/AppData/Local

EDIT2: Here's my ultimate solution:

echo Continuum/anaconda3/envs/*/lib/site-packages/PACKAGENAME | sed -E 's/[^ ]+envs\/([^/]+)\/lib[^ ]+/\1/g' | tr " " "\n"
David Marx
  • 8,172
  • 3
  • 45
  • 66
  • Check this solution https://stackoverflow.com/a/73946853/16349299 or directly [this is Github simple script](https://github.com/mohamadmansourX/random-utilities/blob/main/README.md#listing-environments-containing-set-of-packages), can find envs containing a combination of packages. – mohamadmansourx Oct 04 '22 at 10:58

2 Answers2

3

What about using Anaconda Navigator, there is a list of installed plugins. (Not sure if that would be any faster, loading packages takes ages there sometimes.)

This could work for even deactivated environments:

# conda env list ## to list all environments
conda list -n myenv packagename

(And probably stupid question: simple grep for package name wouldnt work?)

EDIT: Based on your last edit:

Continuum/anaconda3/condabin/conda.bat info --envs | awk '{if ($1 != "#") {print $1}}' | xargs -ix Continuum/anaconda3/condabin/conda.bat list -n x packagename | grep -B 3 packagename

(-B 3 prints 3 lines before match, modified awk command a bit to skip '#' envs. Added packagename to list, otherwise hack with -B 3 wouldnt work)

Dolfa
  • 796
  • 4
  • 21
  • grep could work, not entirely sure where the environments/packages are physically installed though. – David Marx Oct 10 '19 at 17:18
  • I think 'conda info' should return that. There is a similar question discussing it here https://stackoverflow.com/questions/35709497/anaconda-python-where-are-the-virtual-environments-stored – Dolfa Oct 10 '19 at 17:22
  • 1
    `conda env list` will show all environments installed. – William D. Irons Oct 10 '19 at 17:41
  • I'm trying to work through the general strategy you guys are advertising, and it's really non-trivial. Here's what I've got so far: (windows machine, using git bash for the shell terminal): `Continuum/anaconda3/condabin/conda.bat info --envs | awk '{print $2}' | sed 's,C:,/c,g' | sed 's.\\./.g' | xargs -ix ls x` – David Marx Oct 10 '19 at 18:54
  • Ok, this is a bit closer, but it just shows me when I get a hit on the package name and doesn't inform what env it's in. Almost there... `Continuum/anaconda3/condabin/conda.bat info --envs | awk '{print $1}' | xargs -ix Continuum/anaconda3/condabin/conda.bat list -n x | grep packagename` – David Marx Oct 10 '19 at 19:00
  • Did my last edit help or it is not what you are looking for? – Dolfa Oct 21 '19 at 10:30
  • 1
    Would have satisfied my need, but the output is very cluttered. Gave it to @merv for their much simpler solution which also gives a cleaner, more direct output. I like that your approach gives package version as well, which isn't something I need but maybe others will find useful. – David Marx Oct 22 '19 at 17:52
1

If it's a Python package, then a quick and dirty would be

echo Continuum/anaconda3/envs/*/lib/python*/site-packages/packagename

to list every location it is installed (excluding base). If you only want the name then you could extract it...

echo Continuum/anaconda3/envs/*/lib/python*/site-packages/packagename |\
sed -E 's/[^ ]+envs\/([^/]+)\/lib[^ ]+/\1/g'
merv
  • 67,214
  • 13
  • 180
  • 245
  • I like your general approach, but the output I'm getting (from the first echo call) is just the string passed into echo without any wildcard expansion. Using git bash on a windows machine, which I believe is basically MINGW64. – David Marx Oct 22 '19 at 17:37
  • 1
    Got it to work slightly modified: `Continuum/anaconda3/envs/*/lib/site-packages/packageName` – David Marx Oct 22 '19 at 17:44