添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

python缺少ssl

python 没有ssl库的时候,在pypi安装python的一些包的时候,会由于没有ssl/tsl授权,导致链接不上pypi库网站,所以需要安装ssl, 比如下面的错误

1 缺少ssl的错误例子

[bazel-buildfarm]#  bazel build //src/main/java/build/buildfarm:buildfarm-server -s
INFO: Repository pip_deps instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule pip_import defined at:
  /root/.cache/bazel/_bazel_root/74039ce99262255546c3aa91792f4959/external/rules_python/python/pip.bzl:51:29: in <toplevel>
ERROR: An error occurred during the fetch of repository 'pip_deps':
   Traceback (most recent call last):
        File "/root/.cache/bazel/_bazel_root/74039ce99262255546c3aa91792f4959/external/rules_python/python/pip.bzl", line 49, column 13, in _pip_import_impl
                fail("pip_import failed: %s (%s)" % (result.stdout, result.stderr))
Error in fail: pip_import failed: Collecting six==1.15.0 (from -r /root/.cache/bazel/_bazel_root/74039ce99262255546c3aa91792f4959/external/io_bazel_rules_docker/repositories/requirements-pip.txt (line 2))
  Could not fetch URL https://pypi.python.org/simple/six/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/six/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping       
 (pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
  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.")': /simple/six/
  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.")': /simple/six/
  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.")': /simple/six/
  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.")': /simple/six/
  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.")': /simple/six/
  Could not find a version that satisfies the requirement six==1.15.0 (from -r /root/.cache/bazel/_bazel_root/74039ce99262255546c3aa91792f4959/external/io_bazel_rules_docker/repositories/requirements-pip.txt (line 2)) (from versions: )
No matching distribution found for six==1.15.0 (from -r /root/.cache/bazel/_bazel_root/74039ce99262255546c3aa91792f4959/external/io_bazel_rules_docker/repositories/requirements-pip.txt (line 2))

2. 如何确认python少了ssl库呢

[root@xxx bazel-buildfarm]# python
Python 3.7.8 (default, Mar 10 2022, 19:03:25)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3.7/lib/python3.7/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>> exit()

3 安装ssl

3.1 安装python openssl-devel

[root@xxx bazel-buildfarm]# rpm -qa|grep openssl
openssl-libs-1.0.2k-8.el7.x86_64
[root@xxx bazel-buildfarm]# yum install -y penssl-libs-1.0.2k-8.el7.x86_64
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * centos-sclo-rh: mirrors.aliyun.com
 * centos-sclo-sclo: mirrors.nju.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
No package penssl-libs-1.0.2k-8.el7.x86_64 available.
Error: Nothing to do
[root@xxx bazel-buildfarm]# yum install -y openssl-libs-1.0.2k-8.el7.x86_64

3.2 下载python源代码

$ wget -q -O py3.7.8_pkg.tgz https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tgz
$ tar -zxvf py3.7.8_pkg.tgz

3.3 修改ssl配置

Python-3.7.8/Modules/Setup.dist // 如果是新下载的python代码只有这一个文件

Python-3.7.8/Modules/Setup // 如果之前编译过的python代码,还会有这个文件

两个文件都要修改下面这段内容

$ cd Python-3.7.8/Modules
$ vi Setup.dist
    206 # Socket module helper for socket(2)
    207 _socket socketmodule.c               # 打开这一行
    209 # Socket module helper for SSL support; you must comment out the other
    210 # socket line above, and possibly edit the SSL variable:
    211 SSL=/usr/local/ssl                  # 打开这一行
    212 _ssl _ssl.c \                       # 打开这一行
    213         -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \     # 打开这一行
    214         -L$(SSL)/lib -lssl -lcrypto      # 打开这一行

3.4 需要重新编译python

$    ./configure --prefix=/usr/local/python3.7 --enable-shared
$    make && make install

3.5 更新软连接

# 删除之前的软连接:
$ ls -alt /usr/local/bin/python3.7
lrwxrwxrwx 1 root root 32 Mar 10 19:04 /usr/local/bin/python3.7 -> /usr/local/python3.7/bin/python3
ln -s /usr/local/python3.7/bin/python3 /usr/local/bin/python3.7
 $ ll /usr/local/bin/pip3
 lrwxrwxrwx 1 root root 29 Mar 10 19:04 /usr/local/bin/pip3 -> /usr/local/python3.7/bin/pip3
 ln -s /usr/local/python3.7/bin/pip3 /usr/local/bin/pip3
 $ll /usr/local/bin/pip
 lrwxrwxrwx 1 root root 29 Mar 10 19:04 /usr/local/bin/pip -> /usr/local/python3.7/bin/pip3