安装依赖库
yum install -y zlib zlib-dev openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++
修改 Python 安装目录中的 Setup
vim /opt/python3.8/Modules/Setup
将以下五行注释取消
_socket socketmodule.c SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto
设置环境变量 LD_LIBRARY_PATH
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile source $HOME/.bash_profile
重新编译
./configure --prefix=/opt/python3.8 --with-openssl=/usr/local/openssl make && make install
查看效果
(venv) [root@VM-16-16-centos django]# python3 Python 3.8.16 (default, Nov 25 2023, 17:31:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>> exit() (venv) [root@VM-16-16-centos django]#