安装python3.11.7时提示没有模块'_ssl

1 投票
1 回答
80 浏览
提问于 2025-04-14 17:45

这是服务器操作系统的版本

[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

我尝试安装 Python 3.11.7,但失败了,出现了 no module named '_ssl' 的错误。

于是我安装了 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

我安装了 OpenSSL,然后像往常一样安装 Python,但还是失败了。我通常这样做,总是能成功:

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

这次在新服务器上安装时,情况很奇怪。OpenSSL 没有安装在默认的目录 /usr/local/openssl,而是分散安装在 /usr 下的各个位置,和 Python 类似。

比如,通常 OpenSSL 默认安装在 /usr/local/openssl,里面有五个文件夹:bin、include、lib、share 和 ssl。然而,这次它似乎分散到了以下位置:/usr/bin、/usr/include、/usr/lib64,正如你从上面的 whereis 命令结果中看到的。

当我直接输入 openssl version 并得到正确的版本号时,尝试直接安装 python3.11.7 总是提示我 SSL 安装不正确。不过,我似乎无法使用 --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

我在 /usr/lib64/openssl 找到了一个名为 'openssl11' 的文件夹。我尝试用 ./configure --with-openssl=/usr/lib64/openssl/openssl11 --enable-optimizations 来指定 Python 的库文件,但还是失败了。安装后 SSL 仍然无法使用。

这给我带来了很大的麻烦。现在我无法删除已安装的文件,也无法用常规方法配置我的 SSL。有没有人能帮帮我?

1 个回答

0

只需要忽略已经存在的openssl,然后用--prefix选项重新安装openssl,这样可以强制指定安装路径。接着,在安装Python的时候也用--prefix选项。完成这些后,把Python的根路径添加到PATH中,具体是在/etc/profile文件里。这样就解决了我的问题,而且之前的老应用程序也能正常运行。虽然这个方法有点笨,但确实有效。

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

会成功的

撰写回答