Today I learned about pip config require-virtualenv, this option prevents installing packages unless you are in a virtual environment.

There are several ways to set it up: - Via environment variable:

  export PIP_REQUIRE_VIRTUALENV=true
  set PIP_REQUIRE_VIRTUALENV=true  # on Windows
  • Via pip config file:

    pip config set global.require-virtualenv true
  • Via pip config file (~/.config/pip/pip.conf):

    [global]
    require-virtualenv = True

Once you set it up, you will get an error message if you try to install a package without being in a virtual environment:

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

pip error message