从源代码构建 Python 和 OpenSSL,但 ssl 模块失败

Jak*_*aas 2 python linux ssl openssl docker

我正在尝试从容器中的源代码构建 Python 和 OpenSSL。两者似乎都构建正确,但 Python 没有成功创建_ssl模块。

我在网上找到了一些指南 其中提到取消 Python 中的注释和行3.X.X/Modules/Setup,并将--openssldir=/usr/local/ssl标志添加到./configureOpenSSL 的步骤中。我在我的 dockerfile 中执行这些操作。这导致在./configurePython 输出期间,我看到以下行。

checking for X509_VERIFY_PARAM_set1_host in libssl... yes
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

[91m*** WARNING: renaming "_ssl" since importing it failed: /usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by build/lib.linux-x86_64-3.8/_ssl.cpython-38-x86_64-linux-gnu.so)
[0m[91m*** WARNING: renaming "_hashlib" since importing it failed: /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: version `OPENSSL_1_1_1' not found (required by build/lib.linux-x86_64-3.8/_hashlib.cpython-38-x86_64-linux-gnu.so)
[0m
Python build finished successfully!
Run Code Online (Sandbox Code Playgroud)

...

Following modules built successfully but were removed because they could not be imported:
_hashlib              _ssl                                     


Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
Run Code Online (Sandbox Code Playgroud)

如果./configure找到X509...,为什么我仍然收到 hashlib 和 ssl 错误?

完整的 Dockerfile,FWIW:

FROM jenkins/jenkins:lts
USER root
RUN apt-get update && apt-get install -y apt-utils gcc make zlib1g-dev \
    build-essential libffi-dev checkinstall libsqlite3-dev 
RUN wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz && \
    tar xzf openssl-1.1.1d.tar.gz && \
    cd openssl-1.1.1d && \
    ./config -Wl,--enable-new-dtags,-rpath,'$(LIBRPATH)' --prefix=/usr/local/ssl --openssldir=/usr/local/ssl && \
    make && \
    make test && \
    make install
RUN wget -q https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz && \
    tar -xzf Python-3.8.2.tgz && \
    cd Python-3.8.2 && \
    ./configure && \
    make && \
    make install
USER jenkins
Run Code Online (Sandbox Code Playgroud)

kri*_*ina 8

以下内容在 Amazon EC2 上为我工作,默认为 CentOS 7 系统。

首先,CentOS 7 上的 openssl 库太旧(Python 3.9+ 需要 openssl 1.1.1+,可用版本是 1.0.x)。安装较新的:

sudo yum install openssl11-devel
Run Code Online (Sandbox Code Playgroud)

注意:自从写完这个答案后,亚马逊已经终止了 openssl11-devel 软件包并将 openssl-devel 更新到 3.0.8。3.0.8 对于 Python 来说已经足够了,所以现在你可以做yum install openssl-devel.

不幸的是,CentOS 实际上并没有将 SSL 库放在 Python 可以找到的任何地方。经过一些尝试和错误,我发现这让人./configure高兴:

export OPENSSL_LIBS=/usr/lib64/libssl.so
./configure \
  --with-openssl=/usr \
  --with-openssl-rpath=/usr/lib64 \
  --enable-optimizations
Run Code Online (Sandbox Code Playgroud)

解释:

  • 默认情况下,Python 不会在 lib64 中查找,而 lib64 正是 openssl11 的安装位置。
  • --with-openssl采用 ./configure 附加到的路径include/openssl/ssl.h,因此请确保您的系统存在该路径!

当您运行时./configure,您正在寻找输出末尾附近的一行,例如:

checking for stdlib extension module _ssl... yes
Run Code Online (Sandbox Code Playgroud)

如果您看到missing的不是yes,请在 config.log 中搜索 openssl,这应该会为您提供一些有关问题所在的指导。

希望这可以节省其他人我花在解决这个问题上的时间。


Han*_*Han 7

Following modules built successfully but were removed because they could not be imported:
_hashlib              _ssl                                     

Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
Run Code Online (Sandbox Code Playgroud)

从源代码构建 openssl 时,这似乎是安装问题。对于模块上的构建失败_ssl,请尝试使用脚本配置 Python 时的额外选项--with-openssl,例如CFLAGS、 和,例如LDFLAGS./configure

./configure  --with-openssl=/PATH/TO/YOUR/OPENSSL_INSTALL_FOLDER/ \
    --enable-optimizations \
    --with-ssl-default-suites=openssl \
    CFLAGS="-I/PATH/TO/YOUR/OPENSSL_INSTALL_FODLER/include" \
    LDFLAGS="-L/PATH/TO/YOUR/OPENSSL_INSTALL_FODLER/"
Run Code Online (Sandbox Code Playgroud)

openssl version如果它报告如下错误,也请尝试此命令:

/usr/lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found
Run Code Online (Sandbox Code Playgroud)

这意味着您的 openssl 库存在链接问题,我不确定您是否在 Linux 或其他系统上,但对于 Linux 系统,您可以手动修改 openssl 库的链接来解决问题,如我的回答中所述这里

参考

构建 Python 3.7.1 - SSL 模块失败

Python 3.7.0 无法使用 SSL 支持 1.1.0 进行编译