Search code examples
pythonpipanacondacondavirtual-environment

Search anaconda environments for envs where a certain package has been installed


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"

Solution

  • 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'