pip升级报错:
warning: pip is configured with locations that require tls/ssl, however the ssl module in python is not available.
looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
requirement already satisfied: pip in e:\anaconda\install_root\lib\site-packages (21.0.1)
warning: 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/pip/
…
could not fetch url https://pypi.tuna.tsinghua.edu.cn/simple/pip/: there was a problem confirming the ssl certificate: httpsconnectionpool(host=‘pypi.tuna.tsinghua.edu.cn’, 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
在使用 pip 安装 python 包时,常会遇到 pip is configured with locations that require tls/ssl, however the ssl module in python is not available 的警告或错误。这通常是由于 python 环境中的 ssl 模块未正确安装或配置所致。本文详细介绍了常见的解决方法,包括确保正确安装 openssl,必要时从源代码重新构建 python 以包含 ssl 支持,验证 ssl 模块的可用性,通过更新 certifi 包来管理 ca 证书,以及临时忽略 ssl 验证(不推荐)。通过这些方法,您可以有效解决 pip 的 tls/ssl 问题,确保包管理过程顺利安全。
【python】解决python报错:
问题背景
在使用 pip
安装 python 包时,有时会遇到如下警告或错误:
warning: pip is configured with locations that require tls/ssl, however the ssl module in python is not available.
looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
requirement already satisfied: pip in e:\anaconda\install_root\lib\site-packages (21.0.1)
warning: 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/pip/
…
could not fetch url https://pypi.tuna.tsinghua.edu.cn/simple/pip/: there was a problem confirming the ssl certificate: httpsconnectionpool(host=‘pypi.tuna.tsinghua.edu.cn’, 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
这种情况通常意味着当前 python 环境中的 ssl 模块未正确安装或配置,导致 pip
无法处理 https 资源。本文将详细介绍解决这一问题的多种方法,并提供具体的示例和错误代码。
为什么会出现这个警告?
pip
是 python 的包管理工具,在安装包时,它默认使用 https 访问 pypi 存储库。然而,当 ssl 模块在 python 环境中未正确安装或配置时,会导致 pip
无法处理 https 请求,并引发警告或错误。
目录
1. 确保安装了 openssl
python 的 ssl 模块依赖于 openssl 库的支持。首先确保系统上安装了 openssl。
在 linux 上安装 openssl
sudo apt-get update
sudo apt-get install openssl libssl-dev
在 macos 上安装 openssl
使用 homebrew 进行安装:
brew install openssl
brew link openssl --force
2. 从源代码重建 python 解释器
如果你的 python 是从源代码构建的,但在构建时没有找到适当的 openssl 库,你需要重新构建 python,并确保 openssl 正确安装并包含在构建中。
安装依赖
确保构建 python 所需的依赖已安装:
sudo apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev
从源代码重新构建 python
# 下载 python 源代码
wget https://www.python.org/ftp/python/3.x.x/python-3.x.x.tar.xz
tar -xf python-3.x.x.tar.xz
cd python-3.x.x
# 使用 tls 支持配置编译
./configure --with-ssl
# 构建和安装
make
sudo make install
3. 确认 ssl 模块在 python 中可用
启动 python 解释器并尝试导入 ssl
模块以确认其可用性:
python -c "import ssl; print(ssl.openssl_version)"
如果上述命令输出 openssl 的版本信息,则表示 ssl 模块已正确安装。
4. 临时忽略 ssl 验证问题(不推荐)
在极少数情况下,可以选择临时忽略 ssl 验证问题,但这并不是推荐方法,因为它可能带来安全问题。
pip install <package_name> --trusted-host pypi.org --trusted-host files.pythonhosted.org
5. 更新和升级 certifi
包
certifi
包管理着 python 中的 ca 证书集合,用于验证 ssl 连接。如果过时,可能导致 ssl 相关问题。
pip install --upgrade certifi
示例
以下是一个完整的示例,展示如何解决 pip
的 ssl 问题。
模拟错误场景
执行以下命令可能会引发 ssl 模块未安装的错误:
pip install requests
错误信息可能如下:
warning: pip is configured with locations that require tls/ssl, however the ssl module in python is not available. pip is configured with locations that require tls/ssl, however the ssl module in python is not available.
解决方案
安装 openssl
sudo apt-get install openssl libssl-dev
编译并重新安装 python
# 下载并解压 python 源代码
wget https://www.python.org/ftp/python/3.x.x/python-3.x.x.tar.xz
tar -xf python-3.x.x.tar.xz
cd python-3.x.x
# 配置编译环境,确保包含 ssl 支持
./configure --with-ssl
# 构建和安装
make
sudo make install
验证安装
使用以下命令验证 ssl 模块是否正确安装:
python -c "import ssl; print(ssl.openssl_version)"
您应看到类似如下的输出:
openssl 1.1.1 11 sep 2018
结论
通过上述方法,您可以有效解决 pip
在需要 tls/ssl 时的警告与错误问题。确保 openssl 正确安装和配置,重建 python 解释器以包含 ssl 支持,确认 ssl 模块在 python 环境中的可用性,均能帮助您避免并解决 pip
与 ssl 相关的问题。仅在极少数情况中使用忽略 ssl 验证的方法,一般安全考虑下不推荐。
希望这些解决方案能够帮助您顺利进行包管理和依赖安装。如果在操作过程中遇到其他问题或有进一步疑问,欢迎随时交流和讨论!
发表评论