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