7

Since asking a question about Pip vs Package Manager for handling Python Packages I've learned a lot, especially about virtual environments with the venv module. These days I almost exclusively install packages with pip inside virtual environments. I almost rarely install a system-wide python package.

One thing I'm always terrified of, though, is the possibility that I'll run a pip install command without realizing I don't have a venv activated.

Is there a way to blacklist pip only when I'm not in a venv?

3
  • 1
    There is little chance you'll mess up things anyway (unless you work as root). Outside of venv, pip install will require root privileges so you are forced to use --user flag and inside the venv, pip install --user will fail because the user site is not accessible.
    – hoefling
    Jan 4, 2019 at 0:04
  • @hoefling I feel like a moron for not realizing that.
    – mas
    Jan 4, 2019 at 21:21
  • 1
    You shouldn't, it's a good question. You can even uninstall the system pip if your distro supports that, I don't have it installed on my box.
    – hoefling
    Jan 4, 2019 at 22:46

2 Answers 2

5

There's a global option require-virtualenv to require a virtual environment when operating pip.

Several ways to enable this (pick one):

  • As configuration option (which has my preference) like this, in e.g. ~/.config/pip/pip.conf or /etc/pip.conf:

    [global]
    require-virtualenv = True
    

    You can also run this command to modify/create the configuration file for you:

    pip config set global.require-virtualenv True
    
  • Use the environment variable PIP_REQUIRE_VIRTUALENV and set it to value true.

  • Directly using the --require-virtualenv command line option.

If you would then attempt to install packages outside of a virtualenv, you'll see this error:

ERROR: Could not find an activated virtualenv (required).

If you need to override this option temporarily for upgrading pip or installing virtualenv on older Python versions, you can prepend the command with PIP_REQUIRE_VIRTUALENV=false.

N.B.: This option appears to be missing from the --help output and docs as a whole in older pip versions (< 22.0), but it's working just fine for me. Tested with 20.3.4 as latest version on Python 2.7.x.

Sidenote: PEP 704 (Draft at the time of writing) is a proposal to "[...] recommends that package installers like pip require a virtual environment by default on Python 3.13+".

3

You could possibly use a shell alias to take over pip when outside the venv.

alias 'pip=cowsay "You are not in a virtual environment"'

And inside the virtualenv startup file (e.g. the one that gets sourced when you enter the venv):

unalias 'pip'

2
  • Or in a different PATH...but yeah, it is one way of dealing with it. Jan 2, 2019 at 16:00
  • I've never opened the activate script before. Now that I have, that's a pretty snazzy idea.
    – mas
    Jan 2, 2019 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .