Clément Masson
Clément Masson

Reputation: 53

Best practice to manage dependencies between conda and pip

I'm developing a Python library, which depends on multiple packages. I'm struggling to find the most straightforward of managing all those dependencies with the following constraints :

My current setup for the initial install :

At this point, some packages were installed and are now managed by conda, others by pip. When I want to update my environment, I need to:

My problem is that this is unstable : when I update all conda packages, it ensures the consistency of the packages it manages. However, I can't guarantee that the environment as a whole stays consistent, and I just realized that I was missing some updates because I forgot to check for updates in the pip part of the environment.

What's the best way to do this ? I've thought of :

Upvotes: 5

Views: 2956

Answers (1)

merv
merv

Reputation: 76760

The recommendation in the official documentation for managing a Conda environment that also requires PyPI-sourced or pip-installed local packages is to define all dependencies (both Conda and Pip) in a YAML file. Something like:

env.yaml

name: my_env
channels:
 - defaults
dependencies:
 - python=3.8
 - numpy
 - pip
 - pip:
   - some_pypi_only_pkg
   - -e path/to/a/local/pkg

The workflow for updating in such an environment is to update the YAML file (which I would recommend to keep under version control) and then either create a new environment or use

conda env update -f env.yaml

Personally, I would tend to create new envs, rather than mutate (update) an existing one, and use minimal constraints (i.e., >=version) in the YAML. When creating a new env, it should automatically pull the latest consistent packages. Plus, one can keep the previous instances of the env around in case a regression is need during the development lifecycle.

Upvotes: 4

Related Questions