Skip to content

Instantly share code, notes, and snippets.

@robieta
Created March 4, 2023 19:27
Show Gist options
  • Save robieta/82f21ddbf1eafe244515e2dbd7a70872 to your computer and use it in GitHub Desktop.
Save robieta/82f21ddbf1eafe244515e2dbd7a70872 to your computer and use it in GitHub Desktop.
function _helper_conda_available() {
$(command -v conda &> /dev/null) && \
$(command -v conda activate &> /dev/null)
}
function _helper_fail {
printf '%s\n' "FAIL: $1" >&2
: "${__fail_fast:?$1}";
}
function _helper_assert_in_env() {
_helper_conda_available || _helper_fail "Conda is not available."
if [ ! "${CONDA_PREFIX}" ]; then _helper_fail "Not in a conda env."; fi
if [[ "${CONDA_DEFAULT_ENV}" == "base" ]]; then _helper_fail "Do not mess with the base env."; fi
}
function make_clean_env() {
ENV_NAME="$1"
if [ ! "${ENV_NAME}" ]; then
echo "Usage: make_clean_env ENV_NAME [PY_VERSION]"
echo "Create a new conda environment and configure for PyTorch."
echo "Example: make_clean_env my_env 3.8"
fi
# Make sure we are in a clean state. (10 deep should be plenty...)
_helper_conda_available || _helper_fail "Conda is not available"
for i in {1..10}; do
if [ ! -z $CONDA_PREFIX ]; then
conda deactivate || true
fi
done
PY_VERSION="${2:-3.10}"
conda env remove --name "${ENV_NAME}" 2> /dev/null || _helper_fail "Cleanup old env: ${ENV_NAME}"
conda create --no-default-packages -y -n "${ENV_NAME}" python="${PY_VERSION}" || _helper_fail "Make env: ${ENV_NAME}"
# Drop back to base env.
conda deactivate && conda activate
printf "\nENV: ${ENV_NAME}\nPython: ${PY_VERSION}\n\n"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment