pip离线安装和配置pypi国内加速镜像实践

前言

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


pip简介

The PyPA recommended tool for installing Python packages.app

pip安装

https://pip.pypa.io/en/stable/installing/curl

pip在线安装

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
复制代码

pip离线安装

以 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

复制代码

配置pypi国内加速镜像

因为众所周知的缘由,国内访问和下载国外的镜像仓库不顺畅,因此须要作些小小的优化

阿里云(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/

相关文章
相关标签/搜索