How to get `conda env list` to hide an environment without removing it?

AFAIK, there is no way through configuration or otherwise out-of-the-box options to designate specific environments to be ignored by the conda env list command. However, if we look under the hood at how Conda generates this list, we can at least implement a workaround.

Conda User Environment Tracking

Conda tracks environments in two ways:

  1. Environments located in any of the envs_dirs directories are automatically discovered. You can check conda config --show envs_dirs to see which directories that includes. Such environments will not be ignorable in conda env list without altering the internals of how conda-env works (i.e., you’d have to fork the conda code and add new functionality).

  2. Any time a user activates an environment, it gets added to a user-specific tracking file, namely,

    ~/.conda/environments.txt
    

    where ~ is the user home directory. Purging this file of environments you wish to hide should remove the environment from the conda env list output, at least until it is activated again.

Automated Workaround Example

If you’d like a more automated purging, you could include a line in your shell initialization file (e.g., .bash_profile) to remove entries from this file using something like sed and a regex to match the environments you wish to omit.

As a concrete example of this, I frequently encounter this problem as Snakemake user. Snakemake automatically generates Conda environments and uses them to run code in reproducible (-ish) contexts. These environments all get generated under a .snakemake/ directory and eventually start to dominate my conda env list output. This is an absolute bother, since I never intend to manually activate any of these environments, plus they are all named by hashes, so it is practically impossible to recognize their contents by prefix.

To automatically purge these, I can add the following to my .bashrc or .bash_profile:

sed -i '/\.snakemake/d' ~/.conda/environments.txt

This will still lead to transiently showing these auto-generated environments temporarily, but they’ll get purged every time a new shell launches. Hopefully, such transient cases aren’t a major bother, otherwise I imagine more creative solutions to this are also workable, e.g., triggering the purge operation whenever the file is altered.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top