Centos编译Python3.10解决openssl异常

问题描述

在Linux中进行Python应用部署时,安装Python3.10后,在pip安装依赖出现SSLError异常。

(venv) [root@server100 flask-app]# pip install flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
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.")': /pypi/simple/flask/
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.")': /pypi/simple/flask/
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.")': /pypi/simple/flask/
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.")': /pypi/simple/flask/
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.")': /pypi/simple/flask/
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/flask/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Max retries exceeded with url: /pypi/simple/flask/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement flask (from versions: none)
ERROR: No matching distribution found for flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

问题原因

Python3.10引用的openssl是1.1+版本,而当前yum安装的openssl为1.0.2k版本。

解决方法

两种解决方法:

  • 修改configura文件中openssl11的引用
  • 手动编译openssl

前置准备


# 安装python需要的依赖
sudo yum groupinstall "Development Tools" -y
sudo yum install -y openssl-devel bzip2-devel libffi-devel

# uWSGI 需要 EPEL(Extra Packages for Enterprise Linux)仓库。你可以通过以下命令安装 EPEL:
# 编译openssl时也需要
sudo yum install epel-release -y

方法一:yum安装openssl11(成功)

# 配置ssl
yum install -y epel-release.noarch
yum install -y openssl11 openssl11-devel

# 替换openssl为openssl11
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure

# 重新编译,安装
sudo ./configure --enable-optimizations

sudo make altinstall

方法二:手动编译openssl(成功)

安装版本:3.0.14

# 安装perl
yum install -y gcc gcc-c++ zlib-devel libtool autoconf automake perl perl-IPC-Cmd perl-Data-Dumper perl-CPAN

./config --prefix=/usr/local/openssl shared zlib 
make && make install

# 配置LD_LIBRARY_PATH
echo "export LD_LIBRARY_PATH=/usr/local/openssl/lib64:$LD_LIBRARY_PATH" >> ~/.bashrc
echo "export PKG_CONFIG_PATH=/usr/local/openssl/lib64/pkgconfig" >> ~/.bashrc
source ~/.bashrc

# 配置ldconfig
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
echo "/usr/local/openssl/lib64" >> /etc/ld.so.conf
ldconfig -v

# vim  Python-3.10.14/Modules/Setup
# 定位210行,_ssl配置区域
# 如下图所示

./configure --enable-optimizations
sudo make altinstall

  • 注意:编辑Python-3.10.14/Modules/Setup
  • 如果openssl时3.0+,需要使用lib64目录

编辑Modules/Setup文件

参考资料

  1. https://github.com/pypa/pip/issues/10939
  2. https://medium.com/@enahwe/how-to-06bc8a042345
posted @ 2024-11-20 23:03  艳沐石  阅读(18)  评论(1编辑  收藏  举报