原创

pip SSLError 解决方案

问题

当我们使用pip 安装依赖时,有时会出现如下信息:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pymysql
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pymysql/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pymysql/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pymysql/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pymysql/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pymysql/
  Could not fetch URL https://pypi.org/simple/pymysql/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pymysql/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  Could not find a version that satisfies the requirement pymysql (from versions: )
No matching distribution found for pymysql
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

问题原因

查到的部分原因:google说是Python Package Index,不再支持http链接的缘故

pip源码

我们再看看pip的源码:

if WINDOWS:
    bin_py = os.path.join(sys.prefix, 'Scripts')
    bin_user = os.path.join(user_site, 'Scripts')
    # buildout uses 'bin' on Windows too?
    if not os.path.exists(bin_py):
        bin_py = os.path.join(sys.prefix, 'bin')
        bin_user = os.path.join(user_site, 'bin')

    config_basename = 'pip.ini'

    legacy_storage_dir = os.path.join(user_dir, 'pip')
    legacy_config_file = os.path.join(
        legacy_storage_dir,
        config_basename,
    )
else:
    bin_py = os.path.join(sys.prefix, 'bin')
    bin_user = os.path.join(user_site, 'bin')

    config_basename = 'pip.conf'

    legacy_storage_dir = os.path.join(user_dir, '.pip')
    legacy_config_file = os.path.join(
        legacy_storage_dir,
        config_basename,
    )
    # Forcing to use /usr/local/bin for standard macOS framework installs
    # Also log to ~/Library/Logs/ for use with the Console.app log viewer
    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
        bin_py = '/usr/local/bin'

解决方案

windows

进入C盘->用户->Administrator目录;
看是否有pip文件件;如果没有, 则手动创建:

并在pip文件夹下创建pip.ini文件:

并用文本编辑器打开该文件,写入内容:

[global]  
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
disable-pip-version-check = true
timeout = 120  

[list]
format = columns

index-url 这里设置的为豆瓣源
trusted-host 意思是信任这个地址(这就免去了ssl验证)
disable-pip-version-check = true 设置不检查版本

format = columns 这里是设置使用pip list命令时输出的样式,输入pip config list

linux

进入用户目录
看看是否存在.pip文件(.pip 前面的 . 意思是隐藏文件夹)
如果有,则进入,如果没有则创建:

cs ~
cd .pip   或者 mkdir .pip  && cd .pip

查看是否存在pip.conf文件。没有则创建

vim pip.conf

写入内容:

[global]  
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
disable-pip-version-check = true
timeout = 120  

[list]
format = columns

含义和windows一样

然后再回去用pip下载依赖

国内源

可以自己看情况拿下面列出来的源替换掉内容中的 index-url以及host

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/ 

豆瓣:http://pypi.douban.com/simple/

临时使用

如果不想配置以上的东西,还可以使用一次性的方法:
比如我想下载pymysql 依赖:

pip install pymysql http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
正文到此结束
本文目录