Python中pip与conda使用清华镜像


前言

学习需要,在配置某个Python项目时需要使用特定版本(2.2.4)的matplotlib库,于是就想使用Anaconda创建一个虚拟环境,再安装这个包。在Anaconda Navigator的Environments中创建好了一个新的环境,结果在里面搜索安装matplotlib时发现conda中的版本没有2.2.4的,2版本的最高是2.2.3。便想先安装个2.2.3的试一试,结果安装十分缓慢,各种网络错误。无奈之下,只好选择使用镜像。

pypi的国内镜像源:

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

conda使用清华镜像

首先打开Anaconda Prompt,切换到自己的虚拟环境。之后分别输入以下三条命令执行:

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    conda config --set show_channel_urls yes

然后在使用conda进行包的下载安装时将会使用清华镜像,速度会快很多。比如:

conda install matplotlib=2.2.3

然而,尽管这个库下载成功了,项目中程序依旧报错。看来必须要安装2.2.4版本的才可以。不过很奇怪,conda无法安装2.2.4,报这样的错误:

    conda install matplotlib=2.2.4
    Collecting package metadata: done
    Solving environment: failed
     
    PackagesNotFoundError: The following packages are not available from current channels:
     
      - matplotlib=2.2.4

在同学建议下转而使用pip
pip使用清华镜像
1.当次使用

在使用pip install命令时可以在后面加上参数-i https://pypi.tuna.tsinghua.edu.cn/simple,这样在本次安装库时将会选择使用清华镜像来下载,比如

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib==2.2.4

但这样只是这一次生效,如果每次都这样的话将会比较麻烦。
2.永久使用

在Windows环境下,可以直接在当前用户的目录(比如我的目录就是C:\User\Glamour)下新建一个pip目录,在里面新建一个pip.ini文件,写入如下内容:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    [install]
    trusted-host=mirrors.aliyun.com

保存即可。之后使用pip安装包时就快多了。

而且奇怪的是在Anaconda Navigator中看不到已经用pip安装的包:

而使用conda list命令则是可以看到所有的包:

 

    (Draw2) C:\Users\Glamour>conda list
    # packages in environment at C:\Users\Glamour\Anaconda3\envs\Draw2:
    #
    # Name                    Version                   Build  Channel
    ca-certificates           2019.1.23                     0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    certifi                   2019.3.9                 py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    cycler                    0.10.0                   pypi_0    pypi
    decorator                 4.4.0                    pypi_0    pypi
    fa2                       0.3.5                    pypi_0    pypi
    kiwisolver                1.0.1                    pypi_0    pypi
    matplotlib                2.2.4                    pypi_0    pypi
    networkx                  2.2                      pypi_0    pypi
    numpy                     1.16.2                   pypi_0    pypi
    openssl                   1.1.1b               he774522_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pip                       19.0.3                   py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pyparsing                 2.3.1                    pypi_0    pypi
    python                    3.7.2               h8c8aaf0_10    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    python-dateutil           2.8.0                    pypi_0    pypi
    pytz                      2018.9                   pypi_0    pypi
    scipy                     1.2.1                    pypi_0    pypi
    setuptools                40.8.0                   py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    six                       1.12.0                   pypi_0    pypi
    sqlite                    3.27.2               he774522_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    tqdm                      4.31.1                   pypi_0    pypi
    vc                        14.1                 h0510ff6_4    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    vs2015_runtime            14.15.26706          h3a45250_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    wheel                     0.33.1                   py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    wincertstore              0.2                      py37_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

参考

    Anaconda 镜像使用帮助,https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
    让python pip使用国内镜像,https://www.cnblogs.com/wqpkita/p/7248525.html

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用清华镜像安装包,你可以按照以下步骤进行操作: 1. 打开终端或命令提示符窗口。 2. 输入以下命令来配置清华镜像作为conda的源: conda config --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/' conda config --set show_channel_urls yes 这个命令将清华镜像添加到conda的源列表,并设置显示镜像地址。 3. 现在你可以使用conda来安装包了。例如,要安装numpy包,你可以运行以下命令: conda install numpy 这将使用清华镜像下载并安装numpy包。 请注意,以上命令是基于引用\[1\]提供的配置命令。如果你想使用引用\[2\]提供的配置命令,你可以将第一步的命令替换为: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes 这将添加清华镜像的两个通道,并设置显示镜像地址。 如果你需要更多关于使用清华镜像的帮助,你可以参考引用\[3\]提供的链接。 #### 引用[.reference_title] - *1* [conda使用清华大学开源镜像网站下载](https://blog.csdn.net/weixin_41041234/article/details/104279512)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Pythonpipconda使用清华镜像](https://blog.csdn.net/manchengpiaoxue/article/details/103591286)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值