繁体   English   中英

使用 Conda + Poetry 有意义吗?

[英]Does it make sense to use Conda + Poetry?

将 Conda + Poetry 用于机器学习项目是否有意义? 请允许我分享我的(新手)理解并请纠正或启发我:

据我了解,康达诗歌有不同的目的,但在很大程度上是多余的:

  • Conda 主要是一个环境管理器(实际上不一定是 Python),但它也可以管理包和依赖项。
  • Poetry is primarily a Python package manager (say, an upgrade of pip ), but it can also create and manage Python environments (say, an upgrade of Pyenv ).

我的想法是使用两者并划分它们的角色:让 Conda 成为环境经理,让 Poetry 成为 package 经理。 My reasoning is that (it sounds like) Conda is best for managing environments and can be used for compiling and installing non-python packages, especially CUDA drivers (for GPU capability), while Poetry is more powerful than Conda as a Python package manager.

通过在 Conda 环境中使用 Poetry,我设法使这项工作相当容易。 诀窍是不要使用 Poetry 来管理 Python 环境:我没有使用诸如poetry shellpoetry run之类的命令,仅使用poetry initpoetry install等(激活 Conda 环境后)。

为了全面披露,我的environment.yml文件(用于 Conda)如下所示:

name: N

channels:
  - defaults
  - conda-forge

dependencies:
  - python=3.9
  - cudatoolkit
  - cudnn

我的poetry.toml文件看起来像这样:

[tool.poetry]
name = "N"
authors = ["B"]

[tool.poetry.dependencies]
python = "3.9"
torch = "^1.10.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

老实说,我这样做的原因之一是我很难在没有 Conda 的情况下安装 CUDA(用于 GPU 支持)。

你觉得这个项目设计合理吗?

我有使用 Conda + Poetry 设置的经验,并且运行良好。 我的大部分依赖项是在pyproject.toml中指定的,但是当 PyPI 中有一些不可用的东西,或者使用 Conda 安装它更容易时,我将它添加到environment.yml 此外,Conda 用作虚拟环境管理器,与 Poetry 配合得很好:无需使用poetry runpoetry shell ,激活正确的 Conda 环境即可。

创建可重现环境的提示

  1. 添加 Poetry,可能带有版本号(如果需要),作为environment.yml中的依赖项,以便在运行conda create时安装 Poetry,以及 Python 和其他非 PyPI 依赖项。
  2. 添加conda-lock ,它为您提供 Conda 依赖项的锁定文件,就像您拥有 Poetry 依赖项的poetry.lock一样。
  3. 考虑使用通常与conda兼容的mamba ,但更擅长解决冲突,而且速度也更快。 另一个好处是,您设置的所有用户都将使用相同的 package 解析器,独立于本地安装的 Conda 版本。
  4. 默认情况下,使用 Poetry 添加 Python 依赖项。 如果有理由这样做(例如,为了获得支持 CUDA 的版本),请通过 Conda 安装软件包。 在这种情况下,最好在environment.yml中指定包的确切版本,并在安装后,在 Poetry 的pyproject.toml中添加一个具有相同版本规范的条目(版本号前不带^~ )。 这会让 Poetry 知道 package 在那里,不应该升级。
  5. 如果您使用提供相同软件包的不同频道,则可能不清楚将从哪个频道下载特定 package。 一种解决方案是使用:: 表示法为 package 指定通道(参见下面的pytorch条目),另一种解决方案是启用严格通道优先级 不幸的是,在 Conda 4.x 中,无法通过environment.yml启用此选项。
  6. 请注意,Python 将用户站点包添加到sys.path ,如果用户在 Conda 环境之外安装了 Python 包,这可能会导致缺乏可重复性。 一种可能的解决方案是确保将PYTHONNOUSERSITE环境变量设置为True (或任何其他非空值)。

例子

environment.yml

name: my_project_env
channels:
  - pytorch
  - conda-forge
  # We want to have a reproducible setup, so we don't want default channels,
  # which may be different for different users. All required channels should
  # be listed explicitly here.
  - nodefaults
dependencies:
  - python=3.10.*  # or don't specify the version and use the latest stable Python
  - mamba
  - pip  # pip must be mentioned explicitly, or conda-lock will fail
  - poetry=1.*  # or 1.1.*, or no version at all -- as you want
  - tensorflow=2.8.0
  - pytorch::pytorch=1.11.0
  - pytorch::torchaudio=0.11.0
  - pytorch::torchvision=0.12.0

# Non-standard section listing target platforms for conda-lock:
platforms:
  - linux-64

virtual-packages.yml (例如,当我们希望conda-lock即使在没有 CUDA 的平台上也能生成启用 CUDA 的锁文件时,可能会使用它):

subdirs:
  linux-64:
    packages:
      __cuda: 11.5

首次设置

如果您已经在目标环境之外安装了conda-lockmambapoetry ,您可以避免使用 bootstrap env 并简化下面的示例。

# Create a bootstrap env
conda create -p /tmp/bootstrap -c conda-forge mamba conda-lock poetry='1.*'
conda activate /tmp/bootstrap

# Create Conda lock file(s) from environment.yml
conda-lock -k explicit --conda mamba
# Set up Poetry
poetry init --python=~3.10  # version spec should match the one from environment.yml
# Fix package versions installed by Conda to prevent upgrades
poetry add --lock tensorflow=2.8.0 torch=1.11.0 torchaudio=0.11.0 torchvision=0.12.0
# Add conda-lock (and other packages, as needed) to pyproject.toml and poetry.lock
poetry add --lock conda-lock

# Remove the bootstrap env
conda deactivate
rm -rf /tmp/bootstrap

# Add Conda spec and lock files
git add environment.yml virtual-packages.yml conda-linux-64.lock
# Add Poetry spec and lock files
git add pyproject.toml poetry.lock
git commit

用法

上面的设置可能看起来很复杂,但它可以以相当简单的方式使用。

创建环境

conda create --name my_project_env --file conda-linux-64.lock
poetry install

激活环境

conda activate my_project_env

更新环境

# Re-generate Conda lock file(s) based on environment.yml
conda-lock -k explicit --conda mamba
# Update Conda packages based on re-generated lock file
mamba update --file conda-linux-64.lock
# Update Poetry packages and re-generate poetry.lock
poetry update

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM