How to make sure that you only call pip in virtualenv?

How to prevent accidental pip calls when I'm not in virtualenv?

I wrote the following script called pip and added it to my ~/bin (which is before pip in my $PATH ):

 # This script makes sure I don't accidentally install pip without virtualenv # This script requires $PIP to be set to the absolute path of pip to execute pip # if $PIP is not set, it will write a message if [ -z "$PIP" ]; then echo "you are not in a virtual env" echo "use virtual env or" # propose the second item in $PATH echo " export PIP="`type -ap pip|sed -n 2p` echo "to cleanup use" echo " unset PIP" else # execute pip exec $PIP " $@ " fi 

Is there a better way?

+4
source share
2 answers
 export PIP_REQUIRE_VIRTUALENV=true 
+6
source

I would recommend putting this in your ~/.bashrc :

 export PIP_REQUIRE_VIRTUALENV=true 

and you can also add the following function to ~/.bashrc , which allows you to explicitly call pip outside of the virtual environment if you decide:

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

Now you can use your global version of pip to perform actions such as updating virtualenv:

 gpip install --upgrade pip virtualenv 
+3
source

Source: https://habr.com/ru/post/1488600/


All Articles