Skip to content

Instantly share code, notes, and snippets.

@kchawla-pi
Last active July 3, 2024 20:27
Show Gist options
  • Save kchawla-pi/b7d40fcc8b8ed1266e9b427ffffa2526 to your computer and use it in GitHub Desktop.
Save kchawla-pi/b7d40fcc8b8ed1266e9b427ffffa2526 to your computer and use it in GitHub Desktop.
POSIX shell function to deny the install commands if you are in the base environment.
# Source: https://github.com/conda/conda/issues/7791#issuecomment-945676641
# Add this in .bashrc or .zshrc to deny the install commands if you are in the base environment.
# From https://github.com/conda/conda/issues/7791#issuecomment-946537246
# One case where this does not prevent from installing to base:
# If one uses pip in a conda environment, that has no pip installed, it will still install all the packages into the base environment.
# This may happen when an empty environment is created and then one simply runs pip install -r requirements.txt.
# TODO: Do a `which pip` & compare it to conda base env path as an additional check for the pip() function.
function pip(){
if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
echo "Not allowed in base"
else
command pip "$@"
fi
}
function extended_conda(){
if [ "${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