Skip to content

Instantly share code, notes, and snippets.

@marcelm
Created September 17, 2019 09:32
Show Gist options
  • Save marcelm/32dc00cb5e4018670294d2508883402d to your computer and use it in GitHub Desktop.
Save marcelm/32dc00cb5e4018670294d2508883402d to your computer and use it in GitHub Desktop.
Create a Conda environment.lock.yml for macOS while running on Linux
#!/bin/bash
# This script creates both
# - environment.osx.lock.yml and
# - environment.linux.lock.yml
# regardless of the operating system it is running on. The trick is
# temporarily setting the subdir and subdirs keys in .condarc to
# what would be appropriate for the other operating system.
#
# It assumes that there exists a (manually managed) environment.yml file
# that contains the abstract dependencies.
# When creating the test environment, the .lock.yml files are used in order
# to ensure reproducibility.
set -euo pipefail
# We need to temporarily move away the user's .condarc if it exists.
if [[ -e ~/.condarc ]]; then
mv ~/.condarc ~/.condarc.condalock.bak
trap "mv ~/.condarc.condalock.bak ~/.condarc" EXIT
else
trap "rm ~/.condarc" EXIT
fi
for os in linux osx; do
env=temporary-environment-$os
env_yml=environment.$os.lock.yml
printf "subdir: %s-64\nsubdirs:\n - %s-64\n - noarch\n" $os $os >> ~/.condarc
conda env create -n $env -f environment.yml
conda env export -n $env | grep -Ev '^(name|prefix):' > ${env_yml}
conda env remove -n $env
echo "Created ${env_yml}"
done
# The original .condarc will be restored by the exit trap
@marcelm
Copy link
Author

marcelm commented May 25, 2021

This script is outdated, use https://github.com/conda-incubator/conda-lock instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment