V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsgzao
V2EX  ›  Python

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

  •  
  •   wsgzao ·
    wsgzao · 2018-05-04 11:27:59 +08:00 · 2903 次点击
    这是一个创建于 2184 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    pip 安装本身很简单官方推荐的安装方法就一条命令,但离线安装 pip 时就有点痛苦了,因为不知道缺少什么依赖包。有时候我们下载 python 的第三方库入 django 的时候 pip install django 或者 easy_install django 发现下载的速度非常的慢。慢的原因其实就是从 Python 的官方源 pypi.python.org/pypi 下载到本地,然后解包安装。不过因为某些原因,访问官方的 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/

    2 条回复    2018-05-04 15:45:40 +08:00
    lfzyx
        1
    lfzyx  
       2018-05-04 14:42:44 +08:00   ❤️ 1
    Python 的官方源早就换成 https://pypi.org
    wsgzao
        2
    wsgzao  
    OP
       2018-05-04 15:45:40 +08:00
    @lfzyx #1 谢谢提醒,我下面写的新地址,上面还是老地址,现在就更新错误描述
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3218 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:15 · PVG 08:15 · LAX 17:15 · JFK 20:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.