How to Prohibit Global Pip Commands and Only Allow Them in Virtualenv

Nathaniel Mar 08, 2017

Sometime it’s too easy to forget to source bin/activate to activate the virtual environment for your project before running pip install. As a result, you’ll have to spent quite some effort cleaning up the packages you accidentally installed on the system’s native pip.

So, is there a way to automatically reject pip install if there’s no virtualenv detected? The answer is, YES.

Simply add the following lines to .bash_profile (on Mac) or .bashrc (on Ubuntu):

From now on, whenever you pip install something, the terminal will automatically check whether there’s an activated virtualenv. If not, you see some error message like this: