Wednesday, October 26, 2022

[SOLVED] How to prevent pip install without virtualenv?

Issue

I really hate running something like make and then be surprised by pip installing a load of packages because I forgot to activate my virtualenv.

Is there any way to force pip to prompt / warn me that I'm not in some virtualenv?


Solution

Taken from http://docs.python-guide.org/en/latest/dev/pip-virtualenv/

You need to set a environmental variable PIP_REQUIRE_VIRTUALENV

Best practice would be to place it in your autostart file (.bash_profile or similar)

export PIP_REQUIRE_VIRTUALENV=true

To install the package globally, you can then run PIP_REQUIRE_VIRTUALENV="" pip ... or create a command gpip, also in the autostart file:

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
}


Answered By - zalun
Answer Checked By - Marie Seifert (WPSolving Admin)