When multiple people are working on Python project with Git, what is the best way to ensure that the local Conda environments have identical package sets installed?
So far I use
conda env export > conda_env.yml
for recording the environment and
conda env update --file conda_env.yml --prune
for synchronizing an environment.
To verify that the local environment matches the contents of
conda_env.yml
, I have the following test:
def test_conda_environment():
"""Compares output of ``conda env export`` with the contents of ``conda_env.yml``. """
# execute 'conda env export' and parse yaml output:
cmd_result = run(['conda', 'env', 'export'], capture_output=True)
d0 = yaml.safe_load(cmd_result.stdout.decode('utf-8'))
# read saved conda environment from yaml file:
fn = Path('..') / 'conda_env.yml'
with open(fn, 'rt') as fp:
d1 = yaml.safe_load(fp)
# Compare the two dictionaries:
if d0['channels'] != d1['channels']:
print(f"Conda channels differ (current vs '{fn}'): " +
f"{d0['channels']} vs {d0['channels']}")
s0, s1 = sorted(d0['dependencies']), sorted(d1['dependencies'])
if s0 != s1:
df = difflib.Differ()
ds = df.compare(s0, s1)
ts = f"Differences of current environment (+) and file '{fn}' (-):"
print("\n" + ts)
print('=' * len(ts))
print('\n'.join(l_ for l_ in ds if not l_.startswith(' ')))
assert False # fail test
Is there a more straightforward way?