【问题描述】
记录一次使用pip安装模块时总是提示以下错误:
执行类似以下命令安装模块:
pip install xxx
总是报以下错误:
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 (21.2.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: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv 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版本不对应。
执行命令:
python3 -c 'import ssl'
无任何提示说明没问题,若出现类似以下提示,说明就是这个问题了:
这个时候我们就要重新安装python了。此处以安装python3.10.2为例:
1)访问 Python 官方网站,下载Python3.10.2 版本的源代码压缩包。可以使用以下命令:
wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz # 将 "3.10.x" 替换为具体的版本号
2)解压下载的源代码压缩包:
tar -xvf Python-3.10.2.tgz
3)进入解压后的 Python 源代码目录:
cd Python-3.10.2
4)确保已安装openssl1.1.1:
sudo yum install -y openssl11 openssl11-devel
5)在./configure python3之前先做以下动作:
export CFLAGS="-I/usr/include/openssl11" export LDFLAGS="-L/usr/lib64/openssl11"
6)编译和安装 python3:
./configure --prefix=/usr/local/python3 --enable-optimizations make && sudo make altinstall
7)备份旧版本python3:
sudo mv /usr/bin/python3 /usr/bin/python3.old
8)创建指向新安装的 Python 的软链接:
sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
9) 验证python3是否安装成功:
python3 --version
10)最后验证ssl模块是否安装成功:
python3 -c 'import ssl'