pip更换国内镜像源并搭建本地源

1.国内镜像源

阿里云 http://mirrors.aliyun.com/pypi/simple/
豆瓣http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学http://pypi.hustunique.com/

临时使用:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas

永久修改:

Linux下:修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

windows下:直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,然后新建文件pip.ini,即 %HOMEPATH%\pip\pip.ini,在pip.ini文件中输入以下内容(以豆瓣镜像为例):

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

 

2.搭建本地源

首先使用pip下载所有需要的python包到指定的目录内,比如/python-packages

pip download -d /python-packages -r requirement.txt
#pip-requires.txt为需要下载的python包列表

运行脚本init_local_pip.sh /python-packages

#!/bin/bash  
  
# set -x  
  
if [ $# -ne 1 ]; then  
    echo "Usage: $0 packages_dir"  
    exit 1  
fi  
  
[ ! -d $1 ] && echo "Error: you should provide a directory." && exit 1  
  
dest=$1
dest=${dest%/} 
 
if ! echo $dest |grep -q "^/"; then  
    echo "Error: please use the absolute path."  
    exit 1  
fi  
  
if ! ls $dest | egrep -q "(gz|zip)$"; then  
   echo "Note: nothing need to do."  
   exit 0  
fi  
 
#---------------------------------------------
TOPDIR=$(cd $(dirname "$0") && pwd)
tmpdir=`mktemp -d`
#---------------------------------------------
  
for i in `ls ${dest}/{*.gz,*.zip} 2>/dev/null` 
do  
    rm -rf $tmpdir/*  
    cp $i $tmpdir      
    cd $tmpdir  
    package_arch_name=`ls`  
    if echo $package_arch_name | grep -q "gz$"; then  
        tar xf $package_arch_name
        gz_suffix=1  
    else  
        unzip $package_arch_name  
        gz_suffix=0
    fi  
    rm -rf $package_arch_name  
    package_name=`ls`  
    cd $package_name 
    if ls |grep -q "egg-info"; then  
        python setup.py egg_info  
        python setup.py build   
        cd ..  
        if [ $gz_suffix -eq 1 ]; then  
            tar czf $package_arch_name $package_name  
        else  
            zip -r $package_arch_name $package_name   
        fi  
        rm -rf $i           
        cp $package_arch_name $dest/  
    fi  
    cd $TOPDIR
done  
  
rm -rf $tmpdir

安装pypiserver

pip install pypiserver

为了让在系统启动的时候同时启动pypiserver,修改/etc/rc.local

cat /etc/rc.local |egrep -v "^#|^$"
pypi-server /python-packages &>/var/log/pypi-server.log &
exit 0

启动pypiserver

bash /etc/rc.local 

测试

再打开一个虚拟机,ip设置为172.16.1.2,并指定pip源为172.16.1.1

root@ubuntu:~# cat .pip/pip.conf 
[global]
trusted-host = 172.16.1.1
index-url = http://172.16.1.1:8080/simple

2.用pip安装所需python包即可

root@ubuntu:~# pip install -r pip-requires 

原文链接:https://blog.csdn.net/wjciayf/article/details/53813493

posted @ 2019-08-08 16:02  AnswerThe  阅读(6728)  评论(0编辑  收藏  举报