Python教程:在Windows和Linux配置pip国内镜像源【问题02】

pip 国内镜像源配置

  • 1.Windows
  • 1.1 添加文件pip.in
  • 1.2 文件放置
  • 1.3 验证安装
  • 1.4 其他
  • 2.Linux
  • 2.1 使用pip命令更改
  • 2.2 手动更改配置文件
  • 3.问题处理
  • 1.Windows

    1.1 添加文件pip.in

    [global]
    index-url = http://mirrors.aliyun.com/pypi/simple/
    [install]
    trusted-host = mirrors.aliyun.com
    

    1.2 文件放置

    将文件放置到用户 **C:\Users\Administrator\pip **目录下。

    1.3 验证安装

    pip config list
    DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
    global.index-url='http://mirrors.aliyun.com/pypi/simple/'
    install.trusted-host='mirrors.aliyun.com'
    

    1.4 其他

    有些python库需要pip的最新版本才能下载,更新命令:

    python -m pip install --upgrade pip
    

    命令设置:

    pip config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
    
    pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/
    

    国内五大源:

  • 阿里云 http://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣(douban) http://pypi.douban.com/simple/
  • 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  • 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
  • windows:
    直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,在pip 目录下新建文件pip.ini
    或者更快捷的方式:win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件, 内容如下:

    [global]
    timeout = 6000
    index-url=http://pypi.douban.com/simple
    extra-index-url=http://mirrors.aliyun.com/pypi/simple/
    extra-index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
    extra-index-url=http://pypi.mirrors.ustc.edu.cn/simple/
     
    [install]
    trusted-host=pypi.douban.com
    trusted-host=mirrors.aliyun.com
    trusted-host=pypi.tuna.tsinghua.edu.cn
    trusted-host=pypi.mirrors.ustc.edu.cn
    
    # 比较稳定
    [global]
    timeout = 6000
    index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
    extra-index-url=https://pypi.mirrors.ustc.edu.cn/simple/
    

    配置完后添加环境:
    PATH 追加上:%HOMEPATH%\pip\pip.ini
    或者不嫌麻烦的话,又或者默认pip源崩了情况下,每次可以在使用pip的时候在后面加上-i参数,指定pip源

    pip install package-name -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    2.Linux

    2.1 使用pip命令更改

    # 升级pip到最新版本
    pip install --upgrade pip
    
    # 查看源
    pip config get global.index-url
    
    # 设置源
    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
    pip config set global.index-url http://pypi.mirrors.ustc.edu.cn/simple/
    

    2.2 手动更改配置文件

    可以手动更改pip的配置文件,以达到更改镜像源的效果。在用户目录下(Windows系统为C:\Users\用户名,Linux系统为/home/用户名)创建.pip目录,然后在该目录下创建pip.conf文件,写入如下内容:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    

    其中index-url选项表示设置pip的全局镜像源为清华大学的镜像源。

    3.问题处理

    # 报错信息
    WARNING: pip is configured with locations that require TLS/SSL, 
    however the ssl module in Python is not available.
    
    # 安装依赖
    yum install gcc libffi-devel zlib* openssl-devel
    
    # 重新编译安装
    

    作者:シ風箏

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python教程:在Windows和Linux配置pip国内镜像源【问题02】

    发表回复