简体   繁体   中英

Block package installations to conda base environment

I'm currently using miniconda and I want to prevent myself and other users of my machine from installing anything into the base environment. This is because I want users to be creating virtual environments and installing stuff there. I also don't want my base environment to get bloated.

Is there anyway to do this? I use both conda and pip so I imagine I need to somehow block both of those.

One option would be to change the write permissions on the directories pip and conda install packages to for the base environments. These locations vary based on your distribution, but you can check by using something like python -c "import setuptools; print(setuptools.__file__)" . The parent directory to setuputils will be where the packages get installed by default. Run chmod -w <packages dir> to remove write permissions. You can always add them back with chmod +w <packages dir> later, but while they're disabled this should keep you from installing packages there by accident. Unless you haphazardly install packages with sudo , that is...

You can add this in your .bashrc or .zshrc

function pip(){
  if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
    echo "Not allowed in base"
  else
    command pip "$@"
  fi
}

function conda(){
  if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
    echo "Not allowed in base"
  else
    command conda "$@"
  fi
}

It will deny the install commands if you are in the base environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM