pip 鏡像設置方法彙總

pip安裝軟件的時候,常常會遇到模塊的下載速度奇慢無比。後來發現能夠臨時使用鏡像地址ubuntu

例如:bash

sudo pip install -i http://pypi.douban.com/simple/ paramiko  # 使用豆瓣的pip鏡像

有次找ubuntu鏡像源的時候,發現國內的鏡像源不少有pip的鏡像。源站已經很是的強大完善了,感受錯過了好幾億似的。阿里雲

例如:url

  • 中國科技大學鏡像站
http://mirrors.ustc.edu.cn/
  • 阿里雲鏡像站
http://mirrors.aliyun.com/
  • 清華大學鏡像站
https://mirrors.tuna.tsinghua.edu.cn/

不廢話了,直接上方法。code

  • ###臨時使用的方式
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sphinx
  • ###永久使用的方式 在~/.pip/pip.conf文件中添加或修改
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

Use Shell

#!/bin/bash

#auto change pip mirror

PIPPATH=$HOME/.pip
PIPFILE=pip.conf
ROOT_UID=0
ROOT_LOGIN=-1

#create pip conf file and write configer info 2 file
createPipConf(){

        cat >> $PIPPATH/$PIPFILE <<EOF 
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
[list]
format=columns
EOF

echo "create $PIPFILE success!"
}

#if pip path not exists create it
checkPipPath(){

        if [ ! -d $PIPPATH ] ; then
                echo "will create $PIPPATH"
                mkdir -p $PIPPATH
        fi  
}


#must login as normal user
if [ "$(id -u )" -eq "$ROOT_UID" ];then
        echo "you should login as normal user!" 
        exit $ROOT_LOGIN
else        
        checkPipPath
        #if pip.conf exists ,bakup and create a new configer file
        if [ -f $PIPPATH/$PIPFILE ];then
                echo "$PIPFILE exists,will rename 2 $PIPFILE.bak and create a new file!"
                mv $PIPPATH/$PIPFILE $PIPPATH/$PIPFILE.bak
                createPipConf
        else
                echo "will crate pip.conf file!"
                createPipConf
        fi  
fi


exit 0
相關文章
相關標籤/搜索