1、下載Python-3.10.19
Python下載:https://www.python.org/
2、安裝編譯依賴軟件包:
yum groupinstall "Development tools"
yum install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel man
3、編譯安裝Python-3.10.19:
tar -xf Python-3.10.19.tar.xz -C /usr/src/
cd /usr/src/Python-3.10.19
./configure --prefix=/usr/local/python-3.10.19 --enable-shared --with-openssl=/usr
make -j 4
make install
解決報錯:
Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer
Custom linker flags may require --with-openssl-rpath=auto
因Python-3.10.19依賴OpenSSL版本>=1.1.1,推薦最安全、最可控的解決方法是:編譯安裝新版 OpenSSL到獨立目錄(如 /opt/openssl),並僅供 Python 使用:
# 1. 下載新版 OpenSSL (以 1.1.1w 為例,長期支持版本)
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar -xzf openssl-1.1.1w.tar.gz -C /usr/src/
cd /usr/src/openssl-1.1.1w
# 2. 編譯並安裝到獨立目錄
./config --prefix=/opt/openssl --openssldir=/opt/openssl shared zlib
make -j 4
make install
# 3. 回到 Python 源碼目錄,清理並重新配置,指向新版 OpenSSL
cd /usr/src/Python-3.10.19
make distclean
rm -rf build
# 關鍵配置命令:使用 --with-openssl 和 --with-openssl-rpath
./configure --prefix=/usr/local/python-3.10.19 \
--enable-shared \
--with-openssl=/opt/openssl \
--with-openssl-rpath=auto # 自動處理運行時庫路徑,解決鏈接問題
# 4. 重新編譯安裝Python-3.10.19
make -j 4
make install
4、更改默認Python版本為3.10.19:
ln -sf /usr/local/python-3.10.19/bin/python3 /usr/bin/python
ln -sf /usr/local/python-3.10.19/bin/pip3 /usr/bin/pip
5、添加至PATH環境變量:
echo 'export PATH=/usr/local/python-3.10.19/bin:$PATH' >> /etc/profile
source /etc/profile
echo $PATH
6、配置頭文件:
ln -s /usr/local/python-3.10.19/include/python3.10 /usr/include/python-3.10.19
7、配置庫文件(同時包含Python和自定義OpenSSL的庫目錄):
echo "/usr/local/python-3.10.19/lib" > /etc/ld.so.conf.d/python-3.10.19.conf
echo "/opt/openssl/lib" >> /etc/ld.so.conf.d/python-3.10.19.conf
cat /etc/ld.so.conf.d/python-3.10.19.conf
ldconfig
8、查看部署後的Python和pip版本:
python -V
pip -V
測試導入ssl模塊成功:
9、解決yum源的問題:
sed -i '1s#/usr/bin/python$#/usr/bin/python2#' /usr/bin/yum
sed -i '1s#/usr/bin/python$#/usr/bin/python2#' /usr/libexec/urlgrabber-ext-down