Skip to content

Instantly share code, notes, and snippets.

@Mausy5043
Created August 4, 2024 11:14
Show Gist options
  • Save Mausy5043/8e7877f9a7a8da948e31d5e0b7f260f8 to your computer and use it in GitHub Desktop.
Save Mausy5043/8e7877f9a7a8da948e31d5e0b7f260f8 to your computer and use it in GitHub Desktop.
Bash script to create conda environment based on the available `environment.yml` file or create a default environment in cast no YAML file provided.
#!/bin/bash
# MIT License applies.
# Copyright 2024 Maurice (mausy5043) Hendrix
# environment name stored in YAML file takes precedence
if [ -f "environment.yml" ]; then
# YAML exists, so we use that
echo "Found : environment.yml"
env_name=$(grep "name:" environment.yml |cut -d ":" -f 2 |xargs)
else
if [ -z "${1}" ]; then
echo "ERROR: No environment name set!"
exit 1
else
env_name="${1}"
# YAML doesn't exist so we create it now
{
echo "name: ${env_name}"
echo "channels:"
echo " - conda-forge"
echo " - defaults"
echo "dependencies:"
echo " - python=3.11"
} > ./environment.yml
echo "Created : environment.yml"
fi
fi
echo "Creating: ${env_name}"
conda env create --yes --file "environment.yml"
# store the name for the `cg` command
echo "${env_name}" > ./.env
echo "Ready : ${env_name} created."
echo "Activate it before use.
# Usage:
# 1. git clone http://repo
# 2. cd repo
# 3. mkenv repo
# wait for new terminal view to open
# visually check if conda activated the environment
# 4. pycheck
# Tip: save an environment using:
# conda env export --from-history>rescue.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment