pip安裝自己很簡單官方推薦的安裝方法就一條命令,但離線安裝pip時就有點痛苦了,由於不知道缺乏什麼依賴包。有時候咱們下載python的第三方庫入django的時候pip install django 或者 easy_install django 發現下載的速度很是的慢。慢的緣由其實就是從Python的官方源pypi.python.org/pypi 下載到本地,而後解包安裝。不過由於某些緣由,訪問官方的pypi不穩定,很慢甚至有些還時不時的訪問不了。爲了解決這個下載慢的問題,可使用國內的pypi鏡像。python
輕輕鬆鬆解決pip離線安裝,配置pypi國內加速鏡像git
2018年05月03日 - 初稿github
閱讀原文 - https://wsgzao.github.io/post/pip/django
擴展閱讀bootstrap
PyPA - https://www.pypa.io/bash
The PyPA recommended tool for installing Python packages.app
https://pip.pypa.io/en/stable/installing/curl
To install pip, securely download get-pip.py:ide
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
複製代碼
Inspect get-pip.py for any malevolence. Then run the following:gitlab
python get-pip.py
複製代碼
以 Linux 下 Python 2.7.14 和 pip 9.0.1 爲例,Windows 能夠參考最後的推薦連接
下文中提到的壓縮包均可以在官方找到對應的版本 - 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
複製代碼
因爲衆所周知的緣由,國內訪問和下載國外的鏡像倉庫不順暢,因此須要作些小小的優化
阿里雲(aliyun) - https://mirrors.aliyun.com/pypi/simple/ 豆瓣(douban) - https://pypi.douban.com/simple/ 清華大學(tuna) - https://pypi.tuna.tsinghua.edu.cn/simple/
注意,simple 不能少, 是 https 而不是 http
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ansible
複製代碼
pip配置文件不存在則須要手動建立,具體配置信息參考官方文檔
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國內源
# Linux更換pypi國內源
tee ~/.config/pip/pip.conf <<-'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
EOF
複製代碼
Windows更換pypi國內源
# Windows更換pypi國內源,運行如下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國內加速鏡像實踐 - https://wsgzao.github.io/post/pip/ 使用pypiserver快速搭建內網離線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/