18

Is it possible to set the channel_priority to strict when creating an environment using the yaml file? For example:

name: my_environment
channels:
  - conda-forge
dependencies:
  - python
  - geopandas
  - rasterio
Jonne Kleijer
  • 592
  • 5
  • 14
  • this does not seem to be implemented at the moment: https://github.com/conda/conda/issues/8675 – cel Oct 25 '19 at 11:27
  • 1
    It is possible to set channels for each package, e.g., `conda-forge::python`. – merv Nov 18 '19 at 22:05

4 Answers4

17

One additional note is that a specified channel for a given package does not need to be listed in the channels section. I find this safer as it does not risk to (re-)install some other package from unexpected channel.

So, for example:

channels:
  - defaults

dependencies:
  - python =3.8
  - ...
  # specifically from conda-forge (but only those):
  - conda-forge::nbsphinx

Instead of:

# NO!
channels:
  - defaults
  - conda-forge

dependencies:
  - python =3.8
  - ...
  - conda-forge::nbsphinx

Importantly, this seems to install only the specified packages from conda-forge, and it doesn't try to (re-)install conda-forge versions of packages that are in the dependency graph of those, but are already available (perhaps with a slightly less cutting-edge version) from pkgs/main.

Pierre D
  • 24,012
  • 7
  • 60
  • 96
  • "this seems to install only the specified packages from conda-forge, and it doesn't try to (re-)install conda-forge versions of packages that are in the dependency graph of those" -- Unfortunately, this will stop working once `channel_priority: strict` becomes the default in Conda 5. Namely, with `channel_priority: strict` enabled, when I try to install a conda-forge package using your method and one of its dependencies is already installed (from `defaults`), I end up getting a conflict, no matter whether the installed version is actually compatible with the desired version. – balu Feb 17 '21 at 16:49
  • 2
    That being said, I really like your suggestion as it prevents horror stories like https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610 . (Consider that `defaults` is run by a well-known company whereas `conda-forge` does not even provide contact details of anyone in charge. What's worse, several hundred people seem to have access to the `conda-forge` repository.) – balu Feb 17 '21 at 16:53
  • The closest official documentation I could find on this feature is https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-file-manually . Is there anything better? – Kamaraju Kusumanchi May 21 '23 at 02:11
11

Thanks to merv.
A workaround is to specify the channel for each package:

name: my_environment
channels:
  - conda-forge
dependencies:
  - conda-forge::python
  - conda-forge::geopandas
  - conda-forge::rasterio
Jonne Kleijer
  • 592
  • 5
  • 14
1

To directly answer your question, no I don't think it is possible to add channel_priority to the environment file. Maybe raise this as a Conda issue (if it isn't there already ;).

Something else worth trying would be adding defaults to the channel listing explicitly with lower priority like so...

name: my_environment
channels:
  - conda-forge
  - defaults
dependencies:
  - python
  - geopandas
  - rasterio
jakirkham
  • 685
  • 5
  • 18
1

A straightforward way is to first create the empty environment and set the channel priority to strict, then install the packages from the spec file:

conda create new_env
conda activate new_env
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
conda env update --name new_env --file env.yml 

Note: if using a .txt spec file instead of .yml then replace the last line with

conda install --name new_env --file env.txt

reference doc: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#id13

LouisJ Burtz
  • 138
  • 1
  • 4
  • I think that first line should be `conda create -n new_env` – Giles Thomas Nov 25 '22 at 16:12
  • 1
    the purpose of putting this in a YAML file is so that the user does not have to run these command manually as you have shown, which can easily lead to missed steps and an improperly configured environment – user5359531 Apr 11 '23 at 13:22
  • Unfortunately it seems that there simply is no way to do this in YAML (as of time of writing now) – user5359531 Apr 11 '23 at 14:12