Conda: 从基础/根环境中删除所有已安装的包

DR: 如何从 base中删除所有已安装的软件包?

我在我的 base conda 环境中安装了一系列机器学习软件包。

我现在已经为机器学习创建了一个 ml环境,并希望通过删除安装在那里的所有软件包来重置我的 base环境。

我试过了:

% activate base
% conda uninstall -n base --all


CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again

显然,我不能从当前环境中删除包(? !) ,所以让我们先切换到 ml环境:

% source activate ml
% conda uninstall -n base --all


CondaEnvironmentError: cannot remove root environment,
add -n NAME or -p PREFIX option

好吧,那我就用 -p

% conda uninstall -p ~/.local/share/miniconda3 --all


CondaEnvironmentError: cannot remove root environment,
add -n NAME or -p PREFIX option

如何在 baseroot环境中卸载所有已安装的软件包?

136332 次浏览

Apparently, I can't remove packages from the current environment(?!)

It's not that, but instead that you can't remove the base environment, which is what the --all flag does. You can't uninstall all packages in base because that's where the conda executable lives. Instead, what you want to do is uninstall all user-installed packages.

Full Reversion (Not Recommended)

One way to do this is to revert your environment back to the original state before you installed any additional packages:

# Not generally recommended!
conda install --revision 0

Be aware the multiple users have reported this breaking their Conda installation. I definitely would not run this on an installation that you have had for a long time or has many revisions.

Most importantly: Always review the proposed transactions in the base env! This is where Conda lives and unfortunately the safeguards against breaking an installation are not comprehensive.

If you really want a clean start, then export your envs to YAMLs and reinstall a fresh Miniconda.

Partial Reversion

You can also look for other previous states that might be less of a regression, but still get rid of whatever packages you think you've unnecessarily accumulated.

conda list -n base -r

In the end, you'll probably want to upgrade conda right after, since it will also revert any updates to the base packages.


Errors, oh my!

While the above is the correct way to revert, I encounter the error:

CondaRevisionError: Cannot revert to 0, since ::contextlib2-0.5.3-py35_0 is not in repodata.

As an aside, this sort of worries me because it seems to indicate that the state of my Conda environment from two years ago is no longer reproducible from the state of the upstream channels.

Under this situtation, I don't know a clean way to solve this other than comparing all the revision 0 packages to your current install and then uninstalling the difference. But again, a clean install of Miniconda seems like a nicer solution.

Generally, I've found that treating envs as immutable and installing as little as possible in base is the safest and most reliable way to use Conda.

I had the same problem as you did, this is what I did:

  1. backup my conda-env:

    i. activate the env i want keep, such as 'ml'

    ii. type conda-env export > /path/to/save/file/environment.yml

  2. activate base and revert base env to initial: type conda install --revision 0, this should take some time...(this command did not revert ALL my envs, just the BASE env)

  3. load your .yml file: type conda env create -f /path/to/save/file/environment.yml

I used the following command to remove all installed packages (not environment):

$ conda remove `conda list|awk {'print $1'}|tr '\n' ' '`

You could try the following for remove the all installed packages from your anaconda environment,

$ conda list | awk {'print $1'} >> packages

$ for i in `cat packages`; do echo $i; done

$ for i in `cat packages`; do conda remove --force $i -y; done

I've been searching for the best solution to my problem similar to yours for a while on how to remove all packages I installed in the base env. But it turns out the best solution for me is to delete all the packages and envs folder in my disk and uninstall and install the Anaconda again.

simple solution:

select your environment via:

conda activate <env> e.g. conda activate base

use:

pip freeze > requirements.txt

and than:

pip uninstall -r requirements.txt -y

If you just want to remove the unused packages from the deleted environments that are still cached in base, issue

conda clean -a -y

That cleaned up 74 GB from my base :)