Skip to content

Instantly share code, notes, and snippets.

@lynkos
Last active July 6, 2024 22:50
Show Gist options
  • Save lynkos/7a4ce7f9e38bb56174360648461a3dc8 to your computer and use it in GitHub Desktop.
Save lynkos/7a4ce7f9e38bb56174360648461a3dc8 to your computer and use it in GitHub Desktop.
Conda Shortcuts
# Deactivate current conda environment
# Usage: dac
alias dac='conda deactivate'
# Helper function to prompt user for 'Yes' or 'No' response
ask() {
read -p "$@ (Y/N)? " answer
case "${answer}" in
[yY] | [yY][eE][sS])
true
;;
*)
false
;;
esac
}
# Create conda environment(s) from .yml file(s) or CLI
#
# mkenv
# mkenv [file1.yml] ... [fileN.yml]
# mkenv [env_name] [package1] ... [packageN]
# mkenv ... [file1.yml] ... [env_name] [package1] ... [packageN] ... [fileN.yml] ...
mkenv() {
if [ $# == 0 ]; then
conda env create
else
cmd=()
for arg in "$@"; do
case "${arg}" in *.yml | *.YML | *.yaml | *.YAML)
if [ ${#cmd[@]} != 0 ]; then
conda create -n "${cmd[@]}"
unset cmd
fi
[ -f "$arg" ] && conda env create -f "$arg" ||
echo "ERROR: $arg doesn't exist."
;;
*)
cmd+=("${arg}")
;;
esac
done
if [ ${#cmd[@]} != 0 ]; then
conda create -n "${cmd[@]}"
fi
fi
}
# Delete conda environment(s)
# Usage: rmenv [env1] [env2] ... [envN]
rmenv() {
for env in "$@"; do
if ask "Are you sure you want to delete $env"; then
env_path="$(conda info --base)/envs/$env"
[ -e "$env_path" ] &&
conda env remove -n "$env" -y &&
rm -rf "$env_path" ||
echo "ERROR: $env doesn't exist."
fi
done
}
# Rename conda environment
# Usage: rnenv [curr_env_name] [new_name]
rnenv() {
if [ $# == 2 ]; then
if [ "$CONDA_SHLVL" == 0 ]; then
act
conda rename -n "$1" "$2"
dac
else
conda rename -n "$1" "$2"
fi
else
echo "ERROR: Invalid number of args. Must include:"
echo " * Env's current name"
echo " * Env's new name"
fi
}
# Copy conda environment
# Usage: cpenv [orig_env_name] [copy's_name]
cpenv() {
if [ $# == 2 ]; then
conda create -n "$2" --clone "$1"
else
echo "ERROR: Invalid number of args. Must include:"
echo " * Source env's name"
echo " * Env copy's name"
fi
}
# Activate conda environment
# Usage: act [env_name]
act() {
if [ $# == 1 ]; then
conda activate "$1"
elif [ $# == 0 ]; then
conda activate
else
echo "ERROR: Invalid number of args. At most 1 env name is required."
fi
}
# Export [explicit] spec file for building identical conda environments
# Usage: exp [file]
exp() {
if [ $# == 0 ]; then
if ask "Export explicit specs"; then
conda list --explicit > environment.yml
else
conda env export --from-history > environment.yml
fi
elif [ $# == 1 ]; then
if ask "Export explicit specs"; then
conda list --explicit > "$1"
else
conda env export --from-history > "$1"
fi
else
echo "ERROR: Invalid number of args. At most 1 file name is required."
fi
}
# Output [explicit] packages in conda environment
# Usage: lsenv
lsenv() {
if ask "List explicit specs"; then
conda list --explicit
else
conda list
fi
}
@lynkos
Copy link
Author

lynkos commented May 18, 2024

Installation

Add conda_shortcuts.sh to and then source shell startup file (e.g., .bashrc) or restart terminal to apply changes.

  • POSIX
    cat conda_shortcuts.sh >> $HOME/.bashrc
    source $HOME/.bashrc
  • Windows
    type conda_shortcuts.sh >> C:\path\to\.bashrc
    source C:\path\to\.bashrc

Usage

Note

Conda shortcut commands have ONLY been tested on bash v5.2.26(1)-release with aarch64-apple-darwin23.2.0 architecture, so — just to be safe — test and make changes as needed.

E.g., rmenv assumes the path delimeter is forward slash / (POSIX systems); if you use Windows (path delimeter is backslash \), replace forward slashes / in env_path (line 58) with backslashes \.

Command Description Usage
act Activate conda environment

act [env_name]

dac Deactivate conda environment

dac

mkenv Create conda environment(s)

mkenv [yaml_file1] [yaml_file2] ... [yaml_fileN]

mkenv [env_name] [package1] [package2] ... [packageN]

rmenv Remove conda environment(s)

rmenv [env1] [env2] ... [envN]

rnenv Rename conda environment

rnenv [curr_name] [new_name]

cpenv Copy conda environment

cpenv [env_name] [copy's_name]

exp Export conda environment

exp [out_file]

lsenv List conda environment

lsenv

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