Ubuntu20.04升级Python3.10.5后pip安装报SSL模块不可用错误
问题背景
- 操作系统:Ubuntu 20.04
- 操作场景:手动升级Python版本至3.10.5后,调用pip执行安装操作时报错,无法完成包安装流程
- 核心报错信息如下:
Defaulting to user installation because normal site-packages is not writeable WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Requirement already satisfied: pip in /usr/local/lib/python3.10/site-packages (22.0.4) 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/ WARNING: 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/pip/ WARNING: 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/pip/ WARNING: 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/pip/ WARNING: 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/pip/ 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 WARNING: 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
- 故障特征:持续提示Python环境ssl模块不可用,多次重试连接PyPI源均因SSLError失败,无法拉取安装资源。
故障原因
该问题是编译安装Python 3.10.5前未安装OpenSSL开发依赖库,导致编译过程中跳过了ssl模块构建,最终安装的Python运行环境缺少_ssl内置模块。pip默认通过HTTPS协议拉取包资源,依赖ssl模块完成TLS握手,模块缺失时就会触发上述报错。
解决步骤
- 安装Python编译所需的全套依赖,重点包含OpenSSL开发组件
执行以下命令安装依赖:sudo apt update sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev - 重新编译安装Python 3.10.5
进入之前存放Python 3.10.5源码的解压目录(如果之前的源码包已经删除,重新解压对应版本的源码包即可),依次执行以下命令完成重新编译安装:# 进入源码根目录,根据实际存放路径调整 cd Python-3.10.5 # 清理之前的编译缓存,避免旧编译产物干扰 make clean # 配置编译参数,明确启用OpenSSL支持,开启编译优化 ./configure --enable-optimizations --with-ssl-default-suites=openssl # 多线程编译,自动匹配CPU核心数加快编译速度 make -j$(nproc) # 执行altinstall安装,不会覆盖系统默认Python软链接,避免破坏系统依赖 sudo make altinstall - 验证ssl模块可用性
编译安装完成后,执行以下命令验证ssl模块是否正常加载:
命令正常输出OpenSSL版本信息、无模块缺失报错即代表修复成功。python3.10 -c "import ssl; print(ssl.OPENSSL_VERSION)" - 验证pip功能
执行pip升级命令测试功能是否正常:
无SSLError报错、能正常连接源拉取资源即代表问题完全解决。python3.10 -m pip install --upgrade pip
注意事项
- 不要手动修改Ubuntu 20.04系统默认的
/usr/bin/python3软链接,系统大量核心组件依赖默认的Python 3.8环境,强行替换会导致apt、桌面组件等功能异常。日常使用新版本Python直接调用python3.10、pip3.10即可。 - 后续编译更高版本Python前,务必先装齐所有开发依赖,避免出现类似模块缺失问题。
内容的提问来源于stack exchange,提问作者AYUSH DHAR UNOFFICIAL
相关产品推荐
相关产品推荐

