当我不在virtualenv时,如何防止意外调用pip?
我编写了以下名为pip
的脚本,并将其添加到我的~/bin
(我的$PATH
中的pip之前):
# 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
有更好的方法吗?
答案 0 :(得分:6)
export PIP_REQUIRE_VIRTUALENV=true
答案 1 :(得分:3)
我建议将其放在~/.bashrc
文件中:
export PIP_REQUIRE_VIRTUALENV=true
并且您还可以将以下功能添加到~/.bashrc
,以便您可以选择在虚拟环境之外显式调用pip:
gpip() {
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
现在你仍然可以使用你的全局点子版本来做升级virtualenv:
gpip install --upgrade pip virtualenv