Skip to content

Instantly share code, notes, and snippets.

@obscurerichard
Last active December 14, 2024 01:43
Show Gist options
  • Save obscurerichard/4d4c803b64872e5bcd4a28d02e2ed289 to your computer and use it in GitHub Desktop.
Save obscurerichard/4d4c803b64872e5bcd4a28d02e2ed289 to your computer and use it in GitHub Desktop.
Protect the anaconda base environment from accidental installs
# protect-conda-base.sh
#
# If you use Anaconda, it is really easy to accidentally install packages
# into the base environment. Head that off at the pass.
# However, allow installs in a pip virtual environment.
#
# Allow for an override if you run these commands with a CONDA_PROMPT_OVERRIDE variable set,
# as in:
#
# CONDA_PROMPT_OVERRIDE pip install powerline-shell
#
# Include this in your .bashrc or .zshrc
#
# Thanks https://stackoverflow.com/a/69617319/424301
function pip(){
if [ -z "${CONDA_PROMPT_OVERRIDE}" ] && [ -z "${VIRTUAL_ENV}" ] && [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
echo "Not allowed in base"
else
command pip "$@"
fi
}
function extended_conda(){
if [ -z "${CONDA_PROMPT_OVERRIDE}" ] && [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
echo "Not allowed in base"
else
conda "$@"
fi
}
alias conda=extended_conda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment