How To Constrain Package Versions In Conda?
Solution 1:
Package Pinning
Packages can be pinned to specific versions on a per-environment basis. See the documentation on package pinning. For example, suppose we want to pin numpy
and scipy
to the exact versions we currently have in an env called foo
. We could process the output of conda list
to match the expected syntax of the Conda pinning specification:
conda activate foo
conda list "^(numpy|scipy)$" | tail -n+4 | awk '{ print $1 " ==" $2 }' > $CONDA_PREFIX/conda-meta/pinned
A few things to note here:
conda list
takes a regex: use that to your advantagetail
is just to skip the header- this depends on being in the activated env to define
$CONDA_PREFIX
- this overwrites any existing
pinned
file
Freeze Installed
A less labor intensive means of keeping everything constant is to use the --freeze-installed
flag. However, in more recent versions of Conda this is used by default in the first round of solving. So really all this flag does now is to skip the second round of solving that allows for packages that are not part of the explicit specifications to be updated.
PyPI Packages
PyPI packages installed by Pip require some additional configuration to get Conda to pin them. Specifically, one needs the following syntax in the envs/<env>/conda-meta/pinned
file:
numpy=1.21.4=pypi*
That is, indicate that pypi
should be in the build string. And for this to be respected, one must enable Pip interoperability and allow for flexible channel prioritization:
## settings only for this environment
conda activate foo
conda config --env --set pip_interop_enabled True
conda config --env --set channel_priority flexible
Enabling this tells Conda to regard PyPI packages as valid substitutes for solving dependencies.
Personally, I would be careful using this since Conda is a general package manager and sometimes packages installed via Pip (such as yaml
or wget
) do not correspond to the Conda package by the same name. Hence, why I use --env
in the example to only enable this configuration setting within that foo environment.
Post a Comment for "How To Constrain Package Versions In Conda?"