Greg Dubicki
Greg Dubicki

Reputation: 6940

Disable global installs using pip - allow only virtualenvs

Sometimes by mistake, I install some packages globally with plain pip install package and contaminate my system instead of creating a proper virtualenv and keeping things tidy.

How can I easily disable global installs with pip at all?

Or at least show a big warning when using it this way to ensure I know what I am doing?

Upvotes: 14

Views: 2546

Answers (2)

akhomchenko
akhomchenko

Reputation: 280

I can be late, but you can do it either via:

  1. config (~/.pip/pip.conf):
[global]
require-virtualenv = true
  1. env variable PIP_REQUIRE_VIRTUALENV

  2. For pip versions >= 10.0.0b1, from the command line system-wide using:

pip config set global.require-virtualenv true

This gives you exactly what you want, example:

$ pip install foobar
ERROR: Could not find an activated virtualenv (required).

Upvotes: 23

Kieran
Kieran

Reputation: 296

You could try creating adding something like this to your .bashrc

pip() {
    if [ -n "$VIRTUAL_ENV" ]; then
        # Run pip install
    else
        echo "You're not in a virtualenv"
    fi
}

My knowledge of bash isn't the greatest but this should put you on the right path I think.

Upvotes: 0

Related Questions