如何在 MacOS 上使用 python 构建包含 ssl

Lea*_*cim 5 python

在 MacOS 上从源代码构建 python 时,我不小心覆盖了 MacOS 附带的 python,现在它没有 SSL。我尝试通过运行--with-ssl选项再次构建

./configure --with-ssl
Run Code Online (Sandbox Code Playgroud)

但是当我后来跑的时候make,它说了这个

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _ssl               dl              
imageop            linuxaudiodev      ossaudiodev     
readline           spwd               sunaudiodev     
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
Run Code Online (Sandbox Code Playgroud)

通过查看setup.py我应该做什么来找到“必要的位” ,我并不清楚。我该怎么做才能在 MacOS 上使用 SSL 构建 python?

alp*_*ert 6

只需打开setup.py并找到方法detect_modules()。它有一些像(对我来说是 2.7.11):

    # Detect SSL support for the socket module (via _ssl)
    search_for_ssl_incs_in = [
                          '/usr/local/ssl/include',
                          '/usr/contrib/ssl/include/'
                         ]
    ssl_incs = find_file('openssl/ssl.h', inc_dirs,
                         search_for_ssl_incs_in
                         )
    if ssl_incs is not None:
        krb5_h = find_file('krb5.h', inc_dirs,
                           ['/usr/kerberos/include'])
        if krb5_h:
            ssl_incs += krb5_h
    ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
                                 ['/usr/local/ssl/lib',
                                  '/usr/contrib/ssl/lib/'
                                 ] )

    if (ssl_incs is not None and
        ssl_libs is not None):
        exts.append( Extension('_ssl', ['_ssl.c'],
                               include_dirs = ssl_incs,
                               library_dirs = ssl_libs,
                               libraries = ['ssl', 'crypto'],
                               depends = ['socketmodule.h']), )
    else:
        missing.append('_ssl')
Run Code Online (Sandbox Code Playgroud)

所以看起来你需要 SSL 和 Kerberos。Kerberos 随 Mac 一起安装。所以你需要安装openssl. 你可以这样做brew

brew install openssl
Run Code Online (Sandbox Code Playgroud)

openssl头文件可以安装在与 Python 搜索不同的路径中。所以问题

locate ssl.h
Run Code Online (Sandbox Code Playgroud)

并将路径添加到search_for_ssl_incs_in. 例如对我来说是:

/usr/local/Cellar/openssl/1.0.2d_1/include/openssl/ssl.h
Run Code Online (Sandbox Code Playgroud)

所以我应该添加/usr/local/Cellar/openssl/1.0.2d_1/include/search_for_ssl_incs_in.

不要忘记这些是针对 Python 2.7.11 的。但是过程应该是一样的。

希望有帮助。


Mar*_*ers 6

首先,MacOS 只包含 LibreSSL 2.2.7 库,没有头文件,你真的想使用自制软件安装 OpenSSL:

$ brew install openssl
Run Code Online (Sandbox Code Playgroud)

openssl 公式是一个仅限桶的公式,因为 LibreSSL 库隐藏了 OpenSSL,而 Homebrew 不会干扰这一点。这意味着您可以/usr/local/usr/local/opt/openssl. 但是 Homebrew 包含必要的命令行工具来确定要使用的路径。

然后你需要讲述configure这些。如果您正在构建 Python 3.7 或更高版本,请使用--with-openssl开关:

./configure --with-openssl=$(brew --prefix openssl)
Run Code Online (Sandbox Code Playgroud)

如果您正在构建旧版本,请设置CPPFLAGSLDFLAGS环境变量:

CPPFLAGS="-I$(brew --prefix openssl)/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
./configure
Run Code Online (Sandbox Code Playgroud)

Python 配置基础架构从那里获取它。

知道现在古老的Python 版本(2.6 或更早版本,3.0-3.4)仅适用于 OpenSSL 1.0.x 及之前,不再可以从自制内核安装。