红联Linux门户
Linux帮助

搭建pip本地源

发布时间:2016-12-23 10:53:22来源:linux网站作者:wjcaiyf
环境:系统ubuntu14.04.5
eth0: 192.168.10.18   可访问外网 
eth1: 172.16.1.1   不可访问外网
 
一:搭建pip本地源
1.首先使用pip下载所有需要的Python包到指定的目录内,比如/python-packages
root@ubuntu:~# mkdir /python-packages
root@ubuntu:~# cd /python-packages
root@ubuntu:/python-packages# pip download -r ~/pip-requires 
注1:pip-requires为需要下载的python包列表,如下所示
root@ubuntu:/python-packages# cat ~/pip-requires 
# keystone dependencies
pam>=0.1.4
WebOb==1.2.3
eventlet
greenlet
PasteDeploy
paste
routes
sqlalchemy>=0.7.8,<=0.7.9
sqlalchemy-migrate>=0.7.2
passlib
lxml
iso8601>=0.1.4
python-keystoneclient>=0.2.1,<0.3
oslo.config>=1.1.0
注2:如果使用默认的源的话下载很慢,可以换成国内的pypi源,方法如下
root@ubuntu:~# mkdir ~/.pip
root@ubuntu:~# cat ~/.pip/pip.conf 
[global]
trusted-host = mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple
2.运行脚本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 "sorry,you should provide a directory." && exit 1  
dest=$1  
if ! echo $dest |grep -q "^/"; then  
echo "sorry,please use the absolute path."  
exit 1  
fi  
if ! ls $dest | egrep -q "gz$|zip$"; then  
echo "nothing need to do."  
exit 0  
fi  
len_dest=$(expr length $dest)  
lastchar_dest=$(expr substr $dest $len_dest $len_dest)  
if [ "$lastchar_dest" = "/" ]; then  
let "len=$len_dest-1"  
dest=$(expr substr $dest 1 $len)   
fi  
tmpdir=`mktemp`; rm -rf $tmpdir; mkdir -p $tmpdir  
for i in `ls $dest/{*.gz,*.zip}`  
do  
rm -rf $tmpdir/*  
cp $i $tmpdir      
cd $tmpdir  
temp_arch=`ls`  
if echo $temp_arch | grep -q gz; then  
tar xf $temp_arch  
else  
unzip $temp_arch  
fi  
package_name=`ls -l |grep ^d | awk '{print $9}'`  
cd $package_name  
if ls |grep -q "egg-info"; then  
python setup.py egg_info  
python setup.py build   
cd ..  
rm -rf $temp_arch  
if echo $temp_arch |grep -q gz; then  
tar czf $temp_arch $package_name  
else  
zip -r $temp_arch $package_name   
fi  
rm -rf $i           
cp $temp_arch $dest/  
fi  
done  
rm -rf $tmpdir
3.安装pypiserver
root@ubuntu:~# pip install pypiserver
4.为了让在系统启动的时候同时启动pypiserver,修改/etc/rc.local
root@ubuntu:~# cat /etc/rc.local |egrep -v "^#|^$"
cd /var/log/pypiserver
nohup pypi-server -p 8080 /python-packages/ &
exit 0
5.创建pypiserver日志所需目录
root@ubuntu:~# mkdir /var/log/pypiserver
6.启动pypiserver
root@ubuntu:~# bash /etc/rc.local
 
二:测试
1.再打开一个虚拟机,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
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27179.html