5

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

 2 years ago
source link: https://wsgzao.github.io/post/pip/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

pip 安装本身很简单官方推荐的安装方法就一条命令,但离线安装 pip 时就有点痛苦了,因为不知道缺少什么依赖包。有时候我们下载 python 的第三方库入 django 的时候 pip install django 或者 easy_install django 发现下载的速度非常的慢。慢的原因其实就是从 Python 的官方源 https://pypi.org/ 下载到本地,然后解包安装。不过因为某些原因,访问官方的 pypi 不稳定,很慢甚至有些还时不时的访问不了。为了解决这个下载慢的问题,可以使用国内的 pypi 镜像。

轻轻松松解决 pip 离线安装,配置 pypi 国内加速镜像

2018 年 05 月 03 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/pip/

扩展阅读

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 可以参考最后的推荐链接

下文中提到的压缩包都可以在官方找到对应的版本 - 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/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK