Skip to content

Instantly share code, notes, and snippets.

@connerxyz
Last active May 28, 2019 13:55
Show Gist options
  • Save connerxyz/3047e5bb5b647889c234f8b6e7d71443 to your computer and use it in GitHub Desktop.
Save connerxyz/3047e5bb5b647889c234f8b6e7d71443 to your computer and use it in GitHub Desktop.
A script for exporting conda environment definitions to handy files.
#!/bin/bash
# Usage string
usage() {
cat << USAGE
This script exists to serialize a local conda environment.
The output files produced by this script are version-controllable, allowing
for the local conda env definition to included with the repository, distributed,
and recreated on other hosts using either conda *or* pip.
This script expects another file env.activate.sh to exist:
#!/bin/bash
source activate <your-env-name>
Examples:
# conda
conda env create -f environment.yml # env name is specified from 1st line of environment.yml
# venv + pip
python -m venv ./env
surce env/bin/activate
pip install -r requirements.txt # This will not work without manual intervention
Usage:
$0 [-hc]
-h Show usage.
-c Automatically git commit the environment files after writing.
USAGE
}
# CLI arg(s)
help='false'
commit='false'
while getopts 'ch' flag; do
case "${flag}" in
c) commit='true' ;;
h) usage ;;
*) usage
exit 1 ;;
esac
done
# Perform export
echo "🏖️ Activating environment for this project..."
source ./env.activate.sh
echo "📦 Exporting conda env to environment.yml..."
conda env export > environment.yml
echo "📦 Exporting dependencies to requirements.txt..."
conda list -e > requirements.txt
if $commit
then
echo "🔖 Adding environment.yml and requirements.txt to git..."
git add environment.yml
git add requirements.txt
echo "📫 Creating a new commit..."
git commit -m "Updating seralized conda environment definitions."
fi
echo "✨ Complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment