Linux学习36- python3.9出现ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+

发布时间 2023-12-11 11:43:30作者: 上海-悠悠

遇到问题

python3.9上安装requests 库,requests包引入了urllib3,而新版本v2.x的urllib3 需要OpenSSL 1.1.1+以上版本
所以就出现了报错

  File "/root/python39/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 186, in exec_module
    exec(co, module.__dict__)
  File "/root/python39/lib/python3.9/site-packages/pytest_yaml_yoyo/plugin.py", line 6, in <module>
    from requests.adapters import HTTPAdapter
  File "/root/python39/lib/python3.9/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/root/python39/lib/python3.9/site-packages/urllib3/__init__.py", line 41, in <module>
    raise ImportError(
ImportError: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips  26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168

因为我们通过yum安装的openssl-devel版本是1.0.2k

# yum install openssl-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package 1:openssl-devel-1.0.2k-26.el7_9.x86_64 already installed and latest version
Nothing to do

解决问题

解决办法有2种:
第一种是降级urllib3版本即可

pip3 uninstall urllib3
pip3 install urllib3==1.22

这样也就避免了版本冲突问题

第二种办法是升级OpenSSL 1.1.1+以上版本,此方法稍微复杂一些了

wget --no-check-certificate   https://www.openssl.org/source/openssl-1.1.1t.tar.gz
tar -zxvf openssl-1.1.1t.tar.gz
cd openssl-1.1.1t/
./config --prefix=/usr/local/my_openssl
make
make install

安装完成后添加软链接

tar -zxf ./my_openssl.tar.gz -C /usr/local

mv /usr/bin/openssl /usr/bin/oldopenssl
ln -s /usr/local/my_openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/my_openssl/lib/libssl.so.1.1 /usr/lib64/
ln -s /usr/local/my_openssl/lib/libcrypto.so.1.1  /usr/lib64/