Python 3 源码安装过程中无法导入ssl问题解决

参考:

https://techglimpse.com/install-python-openssl-support-tutorial/

https://xu3352.github.io/python/2018/05/15/python-3-install

ssl 解决

直到看到了这篇文章:How to Compile and Install Python with OpenSSL Support?, 然后我尝试着手动源码重新安装了 openssl-1.0.2e 版本, 最终解决该问题

也就是说: yum -y install openssl openssl-devel 安装的 0.9.8e 版本不行!!!

  • 手动安装 openssl-1.0.2e:
    $ cd /tmp
    $ wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
    $ tar xzvf openssl-1.0.2e.tar.gz
    $ cd openssl-1.0.2e
    $ ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
    $ make && make install

    注意:不适用参数编译时, 默认安装目录为: /usr/local/ssl, 这里我们安装到了 /usr/local/openssl, 后面也需要对应的修改

删除老的源码目录, 重新解压一遍!

/usr/local/python3 这个目录我也一起删除掉了

  • 修改 ./setup.py: (默认的openssl路径不改也可以) #### 3.8.1版本的python不需要修改这里,因为没有search_for_ssl_incs_in这个列表
          # Detect SSL support for the socket module (via _ssl)
          search_for_ssl_incs_in = [
                                '/usr/local/openssl/include', # 修改为新目录
                                '/usr/local/openssl/include/openssl',  # 新增
                                '/usr/contrib/ssl/include/'
                               ]
  • 修改 ./Modules/Setup.dist: ###python3.8.1的文件名是Setup,不是Setup.dist
# Socket module helper for socket(2)
_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/openssl   # 这里改为我们指定的目录
_ssl _ssl.c 
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 
    -L$(SSL)/lib -lssl -lcrypto

  

然后重新编译, 安装 (过程参考最上面的安装步骤)

$ ./configure --prefix=/usr/local/python3
$ make
$ make install

检查 ssl 是否安支持, 没报错就是好使了

$ python3
Python 3.6.5 (default, May 17 2018, 21:24:08)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()

  

原文地址:https://www.cnblogs.com/robinunix/p/12881739.html