我可以将频道添加到特定的 conda 环境吗?

Can I add a channel to a specific conda environment?

我想向特定 conda environment 添加一个 conda 通道,但是当我使用

conda config --add channels glotzer

我所有的 conda 环境现在都可以使用该频道。除了测试来自另一个环境的安装之外,~/.condarc 文件还具有以下内容:

channels:
  - glotzer
  - defaults

我将如何配置 conda 以便通道仅在特定环境中可用?

我确实在 channel documentation 中发现对于 conda >= 4.1.0,将通道放在 ~/.condarc 的底部将防止添加的通道覆盖核心包集。

By default conda now prefers packages from a higher priority channel over any version from a lower priority channel. Therefore you can now safely put channels at the bottom of your channel list to provide additional packages that are not in the default channels, and still be confident that these channels will not override the core package set.

我希望这可以防止大多数问题,除非在一个环境中您确实希望通过渠道添加包以覆盖核心包。

更新

截至 2017 年 1 月,无法将通道添加到单个 conda 环境。截至 2020 年 12 月,现在可以按照 .

中的说明进行操作

备选

如果您想从特定渠道安装包,但又不想将该渠道添加到全局 ~/.condarc 文件中,则应使用从特定渠道安装包的选项:

conda install <some-package> -c glotzer

您可以创建一个包含 conda 环境规范的 environment.yml 文件。完整的文档是 here,但基本设置如下:

name: EnvironmentName
channels:
    - conda-forge
    - glotzer
dependencies:
    - pip:
        - tensorflow
    - pandas=0.22.*

要使用环境,请键入

conda env create -f environment.yml
conda activate EnvironmentName

要在 environment.yml 更改或更新包时更新环境,

conda env update -f environment.yml
conda activate EnvironmentName

自 conda 4.2 起,支持特定于环境的 .condarc 文件,您可以编写:

conda config --env --add channels glotzer

将频道添加到活动环境的配置中。

[不确定是否在 4.2 中添加了 --env 标志。答案基于 conda 4.5.9]

您可以创建具有特定频道的新环境:

conda create -n EnvironmentName -c ChannelName