how to include ssl with python build on MacOS

前端 未结 2 1949
别跟我提以往
别跟我提以往 2021-01-06 01:37

While building python from source on a MacOS, I accidntally overwrote the python that came with MacOS, now it doesn\'t have SSL. I tried to build again by running --wi

相关标签:
2条回答
  • 2021-01-06 01:52

    First of all, MacOS only includes LibreSSL 2.2.7 libraries and no headers, you really want to install OpenSSL using homebrew:

    $ brew install openssl
    

    The openssl formula is a keg-only formula because the LibreSSL library is shadowing OpenSSL and Homebrew will not interfere with this. This means that you can find OpenSSL not in /usr/local but in /usr/local/opt/openssl. But Homebrew includes the necessary command-line tools to figure out what path to use.

    You then need to tell configure about these. If you are building Python 3.7 or newer, use the --with-openssl switch:

    ./configure --with-openssl=$(brew --prefix openssl)
    

    If you are building an older release, set the CPPFLAGS and LDFLAGS environment variables:

    CPPFLAGS="-I$(brew --prefix openssl)/include" \
    LDFLAGS="-L$(brew --prefix openssl)/lib" \
    ./configure
    

    and the Python configuration infrastructure takes it from there.

    Know that now ancient Python releases (2.6 or older, 3.0-3.4) only work with OpenSSL 1.0.x and before, which no longer is installable from homebrew core.

    0 讨论(0)
  • 2021-01-06 02:09

    Just open setup.py and find method detect_modules(). It has some lines like (2.7.11 for me):

        # 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')
    

    So it seems that you need SSL and Kerberos. Kerberos comes installed with Mac. So You need to install openssl. You can do it with brew:

    brew install openssl
    

    openssl headers could be installed in a path different than Python will search. So issue

    locate ssl.h
    

    and add the path to search_for_ssl_incs_in. For example for me it is:

    /usr/local/Cellar/openssl/1.0.2d_1/include/openssl/ssl.h
    

    So I should add /usr/local/Cellar/openssl/1.0.2d_1/include/ to search_for_ssl_incs_in.

    Don't forget that these are for Python 2.7.11. But the process should be same.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题