Skip to content

Instantly share code, notes, and snippets.

@lynkos
Last active February 18, 2025 17:57
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
# Confirm by prompting user for yes or no
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 [[ $# -eq 0 ]]; then
conda env create
else
cmd=()
for arg in "$@"; do
case "${arg}" in
*.[yY][mM][lL] | *.[yY][aA][mM][lL])
if [[ ${#cmd[@]} -ne 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[@]} -ne 0 ]]; then
conda create -n "${cmd[@]}"
fi
fi
}
# Delete conda environment(s)
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
rnenv() {
if [[ $# -eq 2 ]]; then
if [[ "$CONDA_SHLVL" -eq 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
cpenv() {
if [[ $# -eq 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
act() {
if [[ $# -eq 1 ]]; then
conda activate "$1"
elif [[ $# -eq 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
exp() {
if [[ $# -eq 0 ]]; then
if ask "Export explicit specs"; then
conda list --explicit > environment.yml
else
conda env export --from-history > environment.yml
fi
elif [[ $# -eq 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
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