前言
pip安裝本身很簡單官方推薦的安裝方法就一條命令,但離線安裝pip時就有點痛苦了旷祸,因為不知道缺少什么依賴包钓试。有時候我們下載python的第三方庫入django的時候pip install django 或者 easy_install django 發(fā)現(xiàn)下載的速度非常的慢。慢的原因其實就是從Python的官方源pypi.python.org/pypi 下載到本地姻乓,然后解包安裝晰搀。不過因為某些原因,訪問官方的pypi不穩(wěn)定办斑,很慢甚至有些還時不時的訪問不了外恕。為了解決這個下載慢的問題,可以使用國內(nèi)的pypi鏡像乡翅。
輕輕松松解決pip離線安裝鳞疲,配置pypi國內(nèi)加速鏡像
更新歷史
2018年05月03日 - 初稿
閱讀原文 - https://wsgzao.github.io/post/pip/
擴(kuò)展閱讀
PyPA - https://www.pypa.io/
pip簡介
The PyPA recommended tool for installing Python packages.
pip安裝
https://pip.pypa.io/en/stable/installing/
pip在線安裝
To install pip, securely download get-pip.py:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Inspect get-pip.py for any malevolence. Then run the following:
python get-pip.py
pip離線安裝
以 Linux 下 Python 2.7.14 和 pip 9.0.1 為例,Windows 可以參考最后的推薦鏈接
下文中提到的壓縮包都可以在官方找到對應(yīng)的版本 - https://pypi.org/
# Install Packages
yum install gcc zlib zlib-devel openssl-devel -y
# Install Python
tar xf Python-2.7.14.tgz
cd Python-2.7.14
./configure
make
make install
cd ..
# ImportError: No module named six.moves
tar xf six-1.11.0.tar.gz
cd six-1.11.0
python setup.py install
cd ..
# ImportError: No module named packaging.version
tar xf packaging-17.1.tar.gz
cd packaging-17.1
python setup.py install
cd ..
# ImportError: No module named pyparsing
tar xf pyparsing-2.2.0.tar.gz
cd pyparsing-2.2.0
python setup.py install
cd ..
# ImportError: No module named appdirs
tar xf appdirs-1.4.3.tar.gz
cd appdirs-1.4.3
python setup.py install
cd ..
# Install Setuptools
unzip setuptools-38.5.2.zip
cd setuptools-38.5.2
python setup.py install
cd ..
# Install pip
tar xf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
cd ..
# Upgrading pip
pip install -U pip
配置pypi國內(nèi)加速鏡像
由于眾所周知的原因蠕蚜,國內(nèi)訪問和下載國外的鏡像倉庫不暢尚洽,所以需要做些小小的優(yōu)化
阿里云(aliyun) - https://mirrors.aliyun.com/pypi/simple/
豆瓣(douban) - https://pypi.douban.com/simple/
清華大學(xué)(tuna) - https://pypi.tuna.tsinghua.edu.cn/simple/
臨時使用
注意,simple 不能少, 是 https 而不是 http
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansible
永久生效
pip配置文件不存在則需要手動創(chuàng)建靶累,具體配置信息參考官方文檔
https://pip.pypa.io/en/stable/user_guide/#config-file
# Linux
~/.config/pip/pip.conf
# Windows
%APPDATA%\pip\pip.ini
# macOS
$HOME/Library/Application Support/pip/pip.conf
Linux更換pypi國內(nèi)源
# Linux更換pypi國內(nèi)源
tee ~/.config/pip/pip.conf <<-'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
EOF
Windows更換pypi國內(nèi)源
# Windows更換pypi國內(nèi)源腺毫,運行以下python代碼會自動建立pip.ini
import os
ini="""[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host=pypi.doubanio.com
"""
pippath=os.environ["USERPROFILE"]+"\\pip\\"
if not os.path.exists(pippath):
os.mkdir(pippath)
with open(pippath+"pip.ini","w+") as f:
f.write(ini)
推薦參考的文章
Python 2.6 升級至 Python 2.7 的實踐心得 - https://wsgzao.github.io/post/python-2-6-update-to-2-7/
pip離線安裝和配置pypi國內(nèi)加速鏡像實踐 - https://wsgzao.github.io/post/pip/
使用pypiserver快速搭建內(nèi)網(wǎng)離線pypi倉庫實踐 - https://wsgzao.github.io/post/pypiserver/
RHEL7/CentOS7在線和離線安裝GitLab配置使用實踐 - https://wsgzao.github.io/post/gitlab/
使用pipenv代替virtualenv管理python包 - https://wsgzao.github.io/post/pipenv/