如何确保仅在virtualenv中调用pip? [英] How to make sure you call pip only in virtualenv?

查看:131
本文介绍了如何确保仅在virtualenv中调用pip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不在virtualenv中时,如何防止意外呼叫pip?

How to prevent accidentally calling pip when I am not in a virtualenv?

我编写了以下脚本,称为pip并将其添加到我的~/bin(在$PATH中的pip之前):

I wrote the following script called pipand 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

有更好的方法吗?

推荐答案

我建议将其放在您的~/.bashrc文件中:

I'd recommend putting this in your ~/.bashrc file:

export PIP_REQUIRE_VIRTUALENV=true

,您还可以在~/.bashrc中添加以下功能,如果您愿意,可以在虚拟环境之外显式调用pip:

and you can also add the following function to your ~/.bashrc that allows you to explicitly call pip outside of a virtual environment if you so choose:

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

现在,您仍然可以使用全局pip版本执行诸如升级virtualenv之类的事情:

Now you can still use your global pip version for doing things like upgrading virtualenv:

gpip install --upgrade pip virtualenv

这篇关于如何确保仅在virtualenv中调用pip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆