Linux環(huán)境運行Python提示No_module_named '_ssl'; ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the ‘ssl‘ module is compiled
python 版本 3.9.6
我是在使用fastApi調(diào)用request模塊時出現(xiàn)的這個問題,
運行程序時報錯
ModuleNotFoundError: No module named '_ssl'
錯誤原因
centos7-2009默認(rèn)openssl版本為1.0.2k,python從3.7.0開始,要求openssl的版本最低要為1.0.2或1.1.0,或者使用libressl來代替openssl
解決一 降低urllib3的版本
錯誤信息 ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the ‘ssl‘ module is compiled
// 1. 卸載
pip uninstall urllib3
// 2. 指定版本重新安裝
pip install urllib3==1.26.15
解決二 升級openssl
下載openssl

我選擇的是1.1.n的版本
安裝
tar -zxvf openssl-1.1.1n.tar.gz
cd openssl-1.1.1n
./config --prefix=/usr/local/openssl
make && make install
修改軟鏈接
mv /usr/bin/openssl /usr/bin/openssl.bak
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
修改相關(guān)文件
echo “/usr/local/openssl/lib” >> /etc/ld.so.conf
# 設(shè)置生效
ldconfig -v
確認(rèn)版本
openssl version
重新編譯安裝python3 看這篇帖子
進入 python3 控制臺,引入ssl 沒有報錯
[root@root]# python3.9
Python 3.9.6 (default, May 13 2025, 18:00:48)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
惜秦皇漢武,略輸文采;唐宗宋祖,稍遜風(fēng)騷。
一代天驕,成吉思汗,只識彎弓射大雕。
俱往矣,數(shù)風(fēng)流人物,還看今朝

浙公網(wǎng)安備 33010602011771號