Python 如何在 conda 虚拟环境中卸载所有未使用的软件包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36308531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:43:19  来源:igfitidea点击:

How to uninstall all unused packages in a conda virtual environment?

pythonpipanacondapackage-managersconda

提问by Erwin Mayer

I have a conda virtual environment with several unused packages installed inside it (either using pip installor conda install).

我有一个 conda 虚拟环境,其中安装了几个未使用的软件包(使用pip installconda install)。

What is the easiest way to clean it up so that only packages that are actually used by my code remain, and the others are uninstalled?

清理它的最简单方法是什么,以便只保留我的代码实际使用的包,而卸载其他包?

回答by kalefranz

conda clean --yes --all

will sanitize everything. But take note: if you ever want to do any type of --offlineoperations, don't use --all; be more selective.

将消毒一切。但请注意:如果您想进行任何类型的--offline操作,请不要使用--all; 更有选择性。

回答by Lorenz

@AgileBean I try an answer to your comment's question on why --packagesgives you more results than --all. This is still related to the main question how to uninstall, hopefully.

@AgileBean 我试着回答你的评论问题,为什么--packages给你的结果比--all. 这仍然与如何卸载的主要问题有关,希望如此。

The difference between

和...之间的不同

    conda clean --yes --all

and

    conda clean --yes --packages

is that the packages are only the extracted folders. All of the other files (.tar.bz2, .conda, that is: tarballs) are not cleaned by --packages.

是包只是提取的文件夹。所有其他文件(.tar.bz2、.conda,即:tarball)都不会被 .tar.gz 清理--packages

If you want to clean only the tarballs, you would need

如果你只想清理压缩包,你需要

    conda clean --yes --tarballs

References: Anaconda Python: Delete .tar.gz in pkgs

参考资料: Anaconda Python:删除 pkgs 中的 .tar.gz

Here is an example of the differences. Mind that --all includes --packages in a real run, but it does not show --packages results in dry-run (very strange!, see the following screenshot, it just stops at DryRunExit: Dry run. Exiting.)

以下是差异的示例。请注意 --all 在实际运行中包含 --packages ,但它不会在试运行中显示 --packages 结果(非常奇怪!,请参见以下屏幕截图,它只是停在DryRunExit: Dry run. Exiting.

<code>conda clean --all --dry-run</code>vs. <code>conda clean --packages --dry-run</code>

<code>conda clean --all --dry-run</code>对比 <code>conda clean --packages --dry-run</code>

Which differences exist that could explain that you find more with --packagesthan with --all?

哪些差异可以解释您发现 with--packages多于 with --all

  1. As said before, my first guess is that you only used dry-run option which will not show you the cleaned --packageswhen you run conda clean --all --dry-run. Therefore see this real run from conda clean --all:

    <code>conda clean --all</code>

  2. The 2 warnings could be interesting:

    WARNING: C:\Users\Admin\.conda\pkgs does not exist                                                                                                                                           
    WARNING: C:\Users\Admin\AppData\Local\conda\conda\pkgs does not exist
    

    But if you do not dry-run, but really run --all, you get the same, because --allincludes the --packagesand thus its warnings as well. This, again, cannot be seen when you use dry-run.

  3. A good reason could be that you have once cleaned your packages with --tarballsor that you have simply removed some tarballs manually so that your unzipped packages outnumber your tarballs in the --dry-run.
  4. You might have unzipped a lot of packages manually into the cache folder, e.g. the manual installations from git and all of the other installations that do not offer conda / pip install and then again, in --dry-run, the --allexits without showing the --packages.
  5. Perhaps you find another thing in the docs? https://docs.conda.io/projects/conda/en/latest/commands/clean.html. It says about symbolic links:
  1. 如前所述,我的第一个猜测是您只使用了dry-run 选项,--packages当您运行conda clean --all --dry-run. 因此,请参阅此真实运行conda clean --all

    <code>conda clean --all</code>

  2. 这两个警告可能很有趣:

    WARNING: C:\Users\Admin\.conda\pkgs does not exist                                                                                                                                           
    WARNING: C:\Users\Admin\AppData\Local\conda\conda\pkgs does not exist
    

    但是如果你没有试运行,而是真正运行--all,你会得到相同的结果,因为--all包括--packages,因此也包括它的警告。当您使用试运行时,这也是看不到的。

  3. 一个很好的理由可能是您曾经使用过清理过您的包,--tarballs或者您只是手动删除了一些 tarball,以便您解压缩的包数量超过 .tar 文件中的 tarball --dry-run
  4. 您可能已经手动将许多软件包解压缩到缓存文件夹中,例如从 git 手动安装和所有其他不提供 conda / pip install 的安装,然后再次在--dry-run--all退出而不显示--packages.
  5. 也许您在文档中发现了另一件事? https://docs.conda.io/projects/conda/en/latest/commands/clean.html。它说到符号链接:

"WARNING: This does not check for packages installed using symlinks back to the package cache."

“警告:这不会检查使用符号链接安装到包缓存的包。”

As --packagesis part of --all, this is still no explanation of your difference.

作为--packages的一部分--all,这仍然无法解释您的差异。

I guess that the reason for your --packages> --allissue is that conda clean --all --dry-rundoes not show the results of the --packages, although it cleans them as well, so that you do not actually have that issue ;).

我猜你的--packages>--all问题的原因是conda clean --all --dry-run没有显示 的​​结果--packages,尽管它也清理了它们,所以你实际上没有那个问题;)。