通过 conda 安装 pip-only 包的依赖项

Installing dependencies for a pip-only package through conda

有时我需要将 pip-only 包安装到 conda 环境中。如果我使用 pip install 安装包,那么该包的所有依赖项都使用 pip 安装, 即使它们可用于 conda.

我想通过 conda 安装尽可能多的包,所以目前我使用 hack 通过 pip 获取包依赖列表,在 conda 上搜索所有包,conda install 找到的,然后用 pip install.

遍历

我更喜欢通过 conda 而不是 pip 安装依赖项是否正确?如果是这样,谁能想出更优雅的方法来解决这个问题?

pip 和 conda 是两个独立的包管理器。只有在极少数情况下,包管理器才真正协同工作。实际应用中conda和pip通常不会。

在现实中,混用conda和pip包通常是不可避免的。正如您所描述的,这通常会导致包管理混乱。

在我看来,解决此问题的最佳且目前唯一正确的方法是为您要在 conda 环境中使用的所有 (pypi-) 包和依赖项创建一个 conda 包。

conda-forge 是一项社区工作,它提供了一种将您自己的包贡献给 conda 基础设施的简单方法。您可能想检查一下您的包裹是否已经可用,如果没有,您是否可以选择贡献。

Am I right to prefer installing dependencies through conda rather than pip?

是的。

当您别无选择只能组合 condapip 时,Anaconda 有一个 blog post that discusses best practices。以下是该博客 post:

中的指南列表

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