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

本文介绍了解决pip在安装Python包时遇到的SSL验证错误的方法。通过修改pip配置文件,指定信任的安装源,避免SSL验证失败导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误代码:

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
效果:
在这里插入图片描述
看到这个效果,证明配置文件已经生效了。

遇到 pip 安装过程中 `_vendor.urllib3.exceptions.SSLError` 错误,特别是 "The operation did not complete (read) at _ssl.c:2559",这通常是由于SSL连接问题,可能是网络问题或者是pip仓库的安全证书问题。以下是一些解决方案: 1. **检查网络连接**:确保你的设备能够正常访问互联网,尝试ping目标URL看能否成功,或者更换其他可靠的网络源再试。 2. **更新SSL库**:有pip会使用较旧的openssl版本导致此问题,尝试升级openssl到最新版,然后重新安装pip: ```sh apt-get update && apt-get install python3-openssl ``` 或者对于macOS用户: ```sh brew upgrade openssl ``` 3. **禁用SSL验证**:如果网络不稳定或者pip仓库的证书有问题,你可以临禁用SSL验证,但这不是一个安全的做法,只适用于调试: ```sh pip install --no-use-pep517 --global-option=--disable-urlfetch-ssl https://...your-package-url... ``` 之后,务必修复这个问题,因为这可能会暴露你的系统于未加密的数据传输风险。 4. **使用国内镜像**:在国内,有直接使用国际的pip源会有上述问题,可以尝试使用阿里云或其他国内提供的pip镜像源,如`pip install --index-url http://mirrors.aliyun.com/pypi/simple/ your_package`。 5. **清除pip缓存**:有pip缓存的问题也可能引发此类错误,可以清理缓存后再尝试: ```sh rm -rf ~/.cache/pip/ ``` 6. **联系软件供应商**:如果以上方法都无法解决问题,可能需要联系软件供应商或pip维护团队,提供详细错误信息进行进一步排查。 请记住,尽量避免使用非官方渠道的包,以免引入安全隐患。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值