Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - conda: remove all installed packages from base/root environment

TL:DR: How can I remove all installed packages from base?

I installed a bunch of machine learning packages in my base conda environment.

I've now created a ml environment for machine learning, and wish to reset my base environment by removing all the packages installed there.

I've tried:

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

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

Apparently, I can't remove packages from the current environment(?!), so lets switch to my ml environment first:

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

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

Orright, I'll use -p then...

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

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

How do I uninstall all installed packages in the base or root environment?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...