安装 Python 包时选择 pip 与 conda 的具体原因

Specific reasons to favor pip vs. conda when installing Python packages

我使用 miniconda 作为默认 python 安装。关于何时使用 conda 与 pip 安装某些东西,当前(2019 年)的智慧是什么?

我通常的行为是使用 pip 安装所有东西,只有在无法通过 pip 获得软件包或 pip 版本无法正常工作时才使用 conda。

总是喜欢conda install有好处吗?混合使用这两个安装程序是否存在问题?我应该考虑哪些因素?


客观性:这不是一个基于意见的问题!我的问题是,当我可以选择安装带有 pipconda 的 python 软件包时,我该如何做出明智的决定?不是 "tell me which is better, but "为什么 我会用一个而不是另一个,来回摆动会导致问题/效率低下吗?

我发现我首先使用 conda 只是因为它会安装二进制文件,而不是如果软件包不存在则尝试 pip。例如,在 conda 中安装 psycopg2 比在 pip 中安装要容易得多。

https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

Pip,代表 Pip Installs Packages,是 Python 官方认可的包管理器,最常用于安装在 Python 包索引 (PyPI) 上发布的包。 pip 和 PyPI 均受 Python 包装管理局 (PyPA) 管理和支持。

简而言之,pip 是 Python 包的通用管理器; conda 是一种与语言无关的跨平台环境管理器。对于用户来说,最显着的区别可能是:pip 在任何环境中安装 python 个包; conda 在 conda 环境中安装任何包。如果您所做的只是在隔离环境中安装 Python 包,conda 和 pip+virtualenv 大部分是可以互换的,只是在依赖处理和包可用性方面存在一些差异。我所说的隔离环境是指 conda-env 或 virtualenv,您可以在其中安装软件包而无需修改系统 Python 安装。

如果我们只关注 Python 包的安装,conda 和 pip 服务于不同的受众和不同的目的。如果您想在现有系统 Python 安装中管理 Python 包,conda 无法帮助您:按照设计,它只能在 conda 环境中安装包。例如,如果您想使用许多依赖外部依赖项的 Python 包(NumPy、SciPy 和 Matplotlib 是常见示例),同时以有意义的方式跟踪这些依赖项,pip 可以无法帮助您:根据设计,它管理 Python 个包并且仅管理 Python 个包。

Conda 和 pip 不是竞争对手,而是针对不同用户群体和使用模式的工具。

我就是这样做的:

  1. 激活您的 conda 虚拟环境
  2. 使用 pip 安装到您的虚拟环境
  3. 如果遇到任何兼容性问题,请使用 conda

我最近 运行 遇到了这个问题,当时 numpy / matplotlib 崩溃了,我使用 conda build 来解决这个问题。

同意 。首先是 Conda,然后是 pip 给定您的 *conda 起点最有意义。

TL;DR 背景故事

Anaconda(发行版)和 Conda(包管理器)旨在解决现状无法解决的安装和集成问题。

这里的现状涵盖了广阔的领域:Python 二进制文件(OS 提供或从 Python.org 下载的任何组合)、系统级包安装程序(例如 apt get、 yum、homebrew)、Python-focused 包安装程序(例如 easy_install 和 pip),以及您可能碰巧使用的设置框架(例如 setuptools 和 distutils)。多年来,这种现状发生了巨大的变化,一些部分(例如 easy_install,distutils)消失了,而新的部分(例如轮子,麻线)出现了。没有看到 JavaScript 生态系统的巨大和持久的变化,但 Python 打包和安装从未真正 "a solved problem," 并且解决问题的首选参赛者在过去有很大差异时间。您可能会争辩说,原生 Python 工具曾经存在的部分或大部分问题现在已基本解决。至少有一些是,但 *conda 社区会非常不同意他们已经黯然失色。

如果您是从 Anaconda 或 miniconda 入手,我想您会喜欢它们的优点(它们可以说更易于安装、更好的集成等)。否则,您可能会选择更多 "stock" 或香草 Python 分布,或者可能选择不同的 "better than the base Python because X, Y, and Z" 分布(例如 ActivePython、Enthought Canopy 等)。鉴于此,我认为您会想先使用 conda,然后再使用 pip,而不是相反。

您当然可以尝试使用 pip 安装每个包,只有当 pip 令人失望时才回退到 conda,但这似乎绕过了您开始并支持 *conda 工具和生态系统的主要选择。

使用 anaconda 生态系统时,您应该始终优先使用 conda 而不是 pip。

docs特别提到这个:(强调我的)

If a package is not available from conda or Anaconda.org, you may be able to find and install the package with another package manager like pip.

Pip packages do not have all the features of conda packages and we recommend first trying to install any package with conda. If the package is unavailable through conda, try installing it with pip. The differences between pip and conda packages cause certain unavoidable limits in compatibility but conda works hard to be as compatible with pip as possible.

使用 conda 包可以帮助您的环境保持一致,特别是如果您需要在同一环境中管理大量依赖项(或者没有环境但直接使用基础环境)。

注意:以下推荐现part of the official documentation.


"What is the current (2019) wisdom regarding when to install something with conda vs. pip?"

Anaconda Inc 的 Jonathan Helmus 在 post“Using Pip in a Conda Environment”中很好地总结了这一点。以下是最终最佳实践建议的摘录:

Best Practices Checklist

Use pip only after conda

  • install as many requirements as possible with conda, then use pip
  • pip should be run with --upgrade-strategy "only-if-needed" (the default)
  • Do not use pip with the --user argument, avoid all “users” installs

Use Conda environments for isolation

  • create a Conda environment to isolate any changes pip makes
  • environments take up little space thanks to hard links
  • care should be taken to avoid running pip in the root [base] environment

Recreate the environment if changes are needed

  • once pip has been used conda will be unaware of the changes
  • to install additional Conda packages it is best to recreate the environment

Store conda and pip requirements in text files

  • package requirements can be passed to conda via the --file argument
  • pip accepts a list of Python packages with -r or --requirements
  • conda env will export or create environments based on a file with conda and pip requirements

作为@eatmeimadanisch 和@merve 推荐的补充 "use conda first, thenn try pip", 这是来自 linux 系统命令行的 运行 对应的代码:

while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt

这假设所有具有所需包号的包都被放入一个名为 "requirements.txt" 的文件中。例如,条目如下所示:

matplotlib==2.0.0
numpy==1.18.1

注意等号是双等号(==),不是单等号(=)。