解决pip安装时出现SSLError的问题

错误代码:

C:\Python27\lib\site-packages\pip\_vendor\urllib3\util\ssl_.py:150: 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/advanced-usage.html#ssl-warnings
  InsecurePlatformWarning
  Could not fetch URL https://pypi.org/simple/pygame/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pygame/ (Caused by SSLError(SSLError(1, '_ssl.c:499: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version'),)) - skipping

出现这个问题的原因是安装源的证书不受信任,可更改pip的配置文件解决这个问题:
更改配置文件首先要知道配置文件在哪,通过阅读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下的路径为: [用户文件夹]/pip/pip.ini
其他系统为:[用户文件夹]/.pip/pip.conf

我是在windows系统下,依次进入 用户 > Administrator
在这里插入图片描述
发现并没有pip文件夹。没关系我们可以自己新建一个文件夹命名为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
效果:
在这里插入图片描述
看到这个效果,证明配置文件已经生效了。

  • 30
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
回答: 当使用pip安装pyopenssl失败,可能是由于以下几个原因导致的。首先,可以尝试更新pip的版本,使用命令python -m pip install --upgrade pip来更新pip。\[1\]另外,可能是由于网络超导致的下载失败,可以尝试使用国内的镜像来下载安装包,例如使用命令pip install pyopenssl -i http://pypi.douban.com/simple --trusted-host pypi.douban.com。\[1\]如果更换默认版本后出现错误,如SyntaxError: invalid syntax,可能是由于系统环境问题导致的,可以尝试解决问题。\[2\]最后,如果出现requests.exceptions.SSLError: HTTPSConnectionPool(host='baike.baidu.com', port=443): Max retries exceeded with url: /item/%E9%A9%AC%E6%97%AD (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))的错误,可能是由于缺少SSL模块导致的,可以尝试安装相应的SSL模块来解决问题。\[3\] #### 引用[.reference_title] - *1* *2* [pip安装插件失败解决方法](https://blog.csdn.net/Lin_Hv/article/details/105121157)[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] - *3* [关于python的openssl报错](https://blog.csdn.net/weixin_44055859/article/details/129488794)[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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值