Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the channel_priority in Conda environment.yaml

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
like image 774
Jonne Kleijer Avatar asked Oct 25 '19 09:10

Jonne Kleijer


People also ask

What is environment Yml in conda?

yml file. Note that by convention Conda environment files are called environment. yml . As such if you use the conda env create sub-command without passing the --file option, then conda will expect to find a file called environment.

How do you adjust conda settings?

To set configuration options, edit the . condarc file directly or use the conda config --set command. For a complete list of conda config commands, see the command reference. The same list is available at the terminal or Anaconda Prompt by running conda config --help .


4 Answers

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
like image 174
Jonne Kleijer Avatar answered Oct 24 '22 19:10

Jonne Kleijer


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.

like image 20
Pierre D Avatar answered Oct 24 '22 21:10

Pierre D


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

like image 28
LouisJ Burtz Avatar answered Oct 24 '22 20:10

LouisJ Burtz


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
like image 27
jakirkham Avatar answered Oct 24 '22 20:10

jakirkham