14

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.

4 Answers 4

17

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 extended_conda(){
  if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
    echo "Not allowed in base"
  else
    conda "$@"
  fi
}
alias conda=extended_conda

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

Sign up to request clarification or add additional context in comments.

2 Comments

What is a .bashrc and where is it located?
.bashrc is a shell configuration file, in your home directory, for bash shell users. For a more detailed look at shell configuration files for bash and zsh see superuser.com/a/1840396
8

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...

2 Comments

This is perfect. Gives me a nice "PermissionError(13, 'Permission denied')" using conda and "Permission denied" using pip.
UPDATE: this caused some unexpected errors with anaconda
4

I am not sure how to lock the environment but suitable solution could be to restore previous environment revision once you detected it was (accidentally) changed. Anytime you can list the environment revisions

conda list --revisions

You will get a list of revisions with their revision number. To revert to particular revision, run:

conda install --revision N

where N is the revision number. In addition, I believe this simple procedure can be automated so you can effectively develop a lock to specific revision. See conda documentation for details.

1 Comment

I wish that this actually worked for me. In the 10 years that I've used conda, I've never once been able to install a previous revision in the base environment. There is always an error of some sort and the only recourse is to reinstall conda from scratch. :-(
0

Based on Gavinkaa's solution, here is a Powershell version:

function Invoke-Pip {
    param (
        [array]
        [Parameter(ValueFromRemainingArguments = $true)]
        $remaining
    )
    if ($env:CONDA_DEFAULT_ENV -eq 'base' -and $remaining[0] -eq 'install') {
        Write-Error -Message "Not allowed in base" -Category PermissionDenied
    } else {
        pip.exe $remaining
    }
}

Set-Alias -Name pip -Value Invoke-Pip

function Invoke-Conda {
    param (
        [array]
        [Parameter(ValueFromRemainingArguments = $true)]
        $remaining
    )
    if ($env:CONDA_DEFAULT_ENV -eq 'base' -and $remaining[0] -eq 'install') {
        Write-Error -Message "Not allowed in base" -Category PermissionDenied
    } else {
        conda.exe $remaining
    }
}

Set-Alias -Name conda -Value Invoke-Conda

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.