签到成功

知道了

CNDBA社区CNDBA社区

Python 更改 PyPI 源

2017-09-30 23:50 6592 3 原创 Python
作者: dave

使用PIP管理Python包非常方便,关于PIP的使用可以参考如下链接:
Linux安装pip及使用
http://www.cndba.cn/Expect-le/article/110

http://www.cndba.cn/cndba/dave/article/2261

但PIP模式用的是https://pypi.python.org/作为源,其访问速度太慢。 所以建议使用国内的PIP源代替。 目前可用替代的PIP源有:

阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 https://pypi.mirrors.ustc.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

指定Pip源有两种方法:

1) 在使用pip时通过-i参数指定,如:
[dave@www.cndba.cn pip-9.0.1]# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:    RedHatEnterpriseServer
Description:    Red Hat Enterprise Linux Server release 6.7 (Santiago)
Release:    6.7
Codename:    Santiago
[dave@www.cndba.cn pip-9.0.1]#
[dave@www.cndba.cn pip-9.0.1]# python -V
Python 2.6.6
[dave@www.cndba.cn pip-9.0.1]# pip install psutil -i https://pypi.mirrors.ustc.edu.cn/simple/
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Collecting psutil
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/d3/0a/74dcbb162554909b208e5dbe9f4e7278d78cc27470993e05177005e627d0/psutil-5.3.1.tar.gz (397kB)
    100% |████████████████████████████████| 399kB 1.6MB/s 
Installing collected packages: psutil
  Running setup.py install for psutil ... done
Successfully installed psutil-5.3.1
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

如果报错,可以再加上—trusted-host选项:http://www.cndba.cn/cndba/dave/article/2261

pip install psutil -i https://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn

这里提示我们,Python 2.6 将不在倍Python 核心团队支持,让我们升级Python版本。 这里我们用的是Linux 6.7。 默认Python版本是2.6.6。 在Linux 7.x中,默认Python的版本就是2.7了。 http://www.cndba.cn/cndba/dave/article/2261

http://www.cndba.cn/cndba/dave/article/2261

2) 配置成默认的源

第一种方法,每次都要指定源,比较麻烦。所以可以通过指定配置文件,来修改默认的PIP源。http://www.cndba.cn/cndba/dave/article/2261

修改配置文件,如不存在,则创建:

linux的文件在~/.pip/pip.conf,
windows在%APPDATA%/pip/pip.ini),http://www.cndba.cn/cndba/dave/article/2261

http://www.cndba.cn/cndba/dave/article/2261
http://www.cndba.cn/cndba/dave/article/2261
http://www.cndba.cn/cndba/dave/article/2261

[dave@www.cndba.cn ~]# mkdir ~/.pip/
[dave@www.cndba.cn ~]# touch ~/.pip/pip.conf

添加以下内容:

[dave@www.cndba.cn ~]# cat ~/.pip/pip.conf 
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

这样在使用pip来安装时,会默认调用该镜像。http://www.cndba.cn/cndba/dave/article/2261

[dave@www.cndba.cn ~]# pip install ab
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
Collecting ab
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading https://mirrors.ustc.edu.cn/pypi/web/packages/a4/6a/7ebdd049b53b1e0084e9ec36fefddc2baaddc53b432caa7fd1d4674de26c/ab-0.3.tar.gz
Installing collected packages: ab
  Running setup.py install for ab ... done
Successfully installed ab-0.3
[dave@www.cndba.cn ~]#

这里成功的使用了USTC的源,而不是默认的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2261
    原创
  • 3
    翻译
  • 578
    转载
  • 191
    评论
  • 访问:8012973次
  • 积分:4346
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ