Search code examples
pythonlinuxsslopensslcentos

install python3.11.7 get no module named '_ssl'


Here is the server OS version

[root@hdp1 bin]# lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.9.2009 (Core)
Release:    7.9.2009
Codename:   Core

I tried to install python 3.11.7 but failed with no module named '_ssl'

So I installed the openssl-1.1.1w

[root@hdp1 bin]# openssl version
OpenSSL 1.1.1w  11 Sep 2023 (Library: OpenSSL 1.1.1k  FIPS 25 Mar 2021)
[root@hdp1 bin]# whereis openssl
openssl: /usr/bin/openssl /usr/lib64/openssl /usr/local/bin/openssl /usr/include/openssl /usr/share/man/man1/openssl.1ssl.gz
[root@hdp1 bin]# which openssl
/usr/local/bin/openssl

I have installed the openssl and then install python as usual, but failed. I usually do this and it always work:

tar -zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config –prefix=/usr/local/openssl shared zlib 
make && make install

tar -zxvf Python-3.11.7.tgz
cd Python-3.11.7
./configure --with-openssl=/usr/local/openssl
make && make altinstall

This time, when installing on a new server, it was very strange. OpenSSL was not installed in the default directory /usr/local/openssl, but was scatteredly installed in various locations under /usr, similar to Python.

For example, typically OpenSSL is installed by default in /usr/local/openssl, and includes the following five folders: bin, include, lib, share, ssl. However, it appears to be scattered to the following locations: /usr/bin, /usr/include, /usr/lib64, as you can also see from the result of the whereis command above.

When I directly input openssl version and get the correct version number, trying to install python3.11.7 directly always tells me that SSL installation is incorrect. However, it seems that I cannot configure it using --with-openssl.

The necessary bits to build these optional modules were not found:
_hashlib              _ssl                  _tkinter           
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

I found a folder named 'openssl11' in /usr/lib64/openssl. I attempted to specify the library files for Python using ./configure --with-openssl=/usr/lib64/openssl/openssl11 --enable-optimizations, but it failed. SSL still cannot be used after installing.

This has caused me significant inconvenience. Now I am unable to delete the installed files and unable to configure my SSL using the usual methods. Can anyone help me?


Solution

  • Just ignore the existing openssl, reinstall openssl with --prefix to force specify the path, and then use --prefix for python installation too. After that, put the python root path to PATH in /etc/profile . Then it solved my case and the existing old applications are still running well. It works in a ugly way...but it works.

    cd openssl-1.1.1w
    clean make
    #just force specify a path ignoring the existing openssl
    ./config –prefix=/usr/local/openssl shared
    make && make install
    
    cd Python3.11.7
    clean make
    #just force specify a path ignoring the existing python and specify the path of openssl just installed
    ./configure –prefix=/usr/local/python3.11 --with-openssl=/usr/local/openssl
    make && make install
    
    vim /etc/profiles
    export PATH=/usr/local/python3.11/bin:$PATH
    source /etc/profiles
    
    python3.11
    import ssl
    

    will succees