如何在Mac OS X上安装密码学?

11

我的操作系统:Mac OS X El Capitan

当我像这样安装密码学模块:

pip install cryptography==0.2.1

错误是:
Collecting cryptography==0.2.1
  Using cached cryptography-0.2.1.tar.gz
    Complete output from command python setup.py egg_info:
    no previously-included directories found matching 'documentation/_build'
    zip_safe flag not set; analyzing archive contents...
    six: module references __path__

    Installed /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/six-1.10.0-py2.7.egg
    Searching for cffi>=0.8
    Reading https://pypi.python.org/simple/cffi/
    Best match: cffi 1.5.0
    Downloading https://pypi.python.org/packages/source/c/cffi/cffi-1.5.0.tar.gz#md5=dec8441e67880494ee881305059af656
    Processing cffi-1.5.0.tar.gz
    Writing /var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/easy_install-ZAtQYR/cffi-1.5.0/setup.cfg
    Running cffi-1.5.0/setup.py -q bdist_egg --dist-dir /var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/easy_install-ZAtQYR/cffi-1.5.0/egg-dist-tmp-Dl08xt
    creating /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg
    Extracting cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg to /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs

    Installed /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg
    Searching for pycparser
    Reading https://pypi.python.org/simple/pycparser/
    Best match: pycparser 2.14
    Downloading https://pypi.python.org/packages/source/p/pycparser/pycparser-2.14.tar.gz#md5=a2bc8d28c923b4fe2b2c3b4b51a4f935
    Processing pycparser-2.14.tar.gz
    Writing /var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/easy_install-80l9Lo/pycparser-2.14/setup.cfg
    Running pycparser-2.14/setup.py -q bdist_egg --dist-dir /var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/easy_install-80l9Lo/pycparser-2.14/egg-dist-tmp-5dC3Ib
    warning: no previously-included files matching 'yacctab.*' found under directory 'tests'
    warning: no previously-included files matching 'lextab.*' found under directory 'tests'
    warning: no previously-included files matching 'yacctab.*' found under directory 'examples'
    warning: no previously-included files matching 'lextab.*' found under directory 'examples'
    zip_safe flag not set; analyzing archive contents...
    Copying pycparser-2.14-py2.7.egg to /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs

    Installed /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/pycparser-2.14-py2.7.egg
    running egg_info
    creating pip-egg-info/cryptography.egg-info
    writing requirements to pip-egg-info/cryptography.egg-info/requires.txt
    writing pip-egg-info/cryptography.egg-info/PKG-INFO
    writing top-level names to pip-egg-info/cryptography.egg-info/top_level.txt
    writing dependency_links to pip-egg-info/cryptography.egg-info/dependency_links.txt
    writing manifest file 'pip-egg-info/cryptography.egg-info/SOURCES.txt'
    warning: manifest_maker: standard file '-c' not found

    cryptography/hazmat/bindings/__pycache__/_cffi__xfdb41ad9xb4fc9075.c:369:5: error: conflicting types for 'ASN1_GENERALIZEDTIME_check'
    int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *);
        ^
    /usr/local/include/openssl/asn1.h:856:5: note: previous declaration is here
    int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *a);
        ^
    1 error generated.
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/setup.py", line 113, in <module>
        "build": cffi_build,
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/core.py", line 151, in setup
        dist.run_commands()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/egg_info.py", line 180, in run
        self.find_sources()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/egg_info.py", line 207, in find_sources
        mm.run()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/egg_info.py", line 291, in run
        self.add_defaults()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/egg_info.py", line 320, in add_defaults
        sdist.add_defaults(self)
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/sdist.py", line 118, in add_defaults
        build_py = self.get_finalized_command('build_py')
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/cmd.py", line 312, in get_finalized_command
        cmd_obj.ensure_finalized()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/cmd.py", line 109, in ensure_finalized
        self.finalize_options()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/site-packages/setuptools/command/build_py.py", line 28, in finalize_options
        orig.build_py.finalize_options(self)
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/command/build_py.py", line 46, in finalize_options
        ('force', 'force'))
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/cmd.py", line 298, in set_undefined_options
        src_cmd_obj.ensure_finalized()
      File "/Users/username/.pyenv/versions/2.7.11/lib/python2.7/distutils/cmd.py", line 109, in ensure_finalized
        self.finalize_options()
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/setup.py", line 55, in finalize_options
        OpenSSLBinding().ffi.verifier.get_extension(),
      File "cryptography/hazmat/bindings/openssl/binding.py", line 83, in __init__
        self._ensure_ffi_initialized()
      File "cryptography/hazmat/bindings/openssl/binding.py", line 99, in _ensure_ffi_initialized
        libraries)
      File "cryptography/hazmat/bindings/utils.py", line 72, in build_ffi
        ext_package="cryptography",
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg/cffi/api.py", line 433, in verify
        lib = self.verifier.load_library()
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg/cffi/verifier.py", line 110, in load_library
        self._compile_module()
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg/cffi/verifier.py", line 207, in _compile_module
        outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg/cffi/ffiplatform.py", line 29, in compile
        outputfilename = _build(tmpdir, ext, compiler_verbose)
      File "/private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography/.eggs/cffi-1.5.0-py2.7-macosx-10.11-x86_64.egg/cffi/ffiplatform.py", line 62, in _build
        raise VerificationError('%s: %s' % (e.__class__.__name__, e))
    cffi.ffiplatform.VerificationError: CompileError: command 'clang' failed with exit status 1

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/48/lrr_y58s4rq70qdnjcvqc8l8j2g31b/T/pip-build-zGO9s0/cryptography

我确定已经将openssl升级到最新版本:

$ openssl version
OpenSSL 1.0.2f  28 Jan 2016

但是我不知道如何修复它。即使我查看了官方文档:

https://cryptography.io/en/latest/installation/#building-cryptography-on-os-x

并按照那样做,结果仍然一样。

1
你确定你的Python打包工具链是最新的吗?当我在El Capitan上使用pip安装cryptography时,它只是下载并安装了一个.whl(二进制)包。你使用的pip和setuptools版本是什么? - wjv
pip: 8.0.2; setuptools 18.2. - ithelloworld
我刚刚创建了一个新的空白虚拟环境,并尝试在其中安装密码学0.2.1...但是我得到了相同的错误。(安装最新版本1.2.2非常顺利。我假设您有想要旧版本的充分理由?)我现在将逐步解决这个错误,看看能否找到问题的根源... - wjv
无论如何,问题是由于OpenSSL头文件(虽然不是库本身)已从El Capitan中删除,作为苹果持续(自10.7以来)弃用OpenSSL而采用自己的加密服务的一部分。理论上,应该能够使用(比如)Homebrew安装的OpenSSL版本构建这些Python模块。我正在尝试 - 到目前为止失败了。 - wjv
非常感谢您的回答。我的项目长期使用加密0.2.1版本,我们还没有升级它。我认为您是正确的。问题出在OpenSSL和Mac OS X上。 - ithelloworld
3个回答

14

brew 来拯救:

brew install openssl

然后brew给出了一个提示:

苹果已经弃用OpenSSL,转而使用自己的TLS和加密库。

通常这对你来说没有任何影响。如果你构建自己的软件,并且它需要这个配方,你需要添加到你的构建变量中:

LDFLAGS:  -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

因此,只需导出这些环境变量,然后重新运行您喜欢的pip install(或者更好地避免将来可能发生的混乱,将以下内容添加到您的〜/ .bash_profile :

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

这个对我来说非常有效!


6

我正在尝试在Mac OS El Capitan上安装Scrapy,需要安装密码学包。如密码学安装文档所述。

env LDFLAGS="-L$(brew --prefix openssl)/lib" CFLAGS="-I$(brew --prefix openssl)/include" pip install cryptography

-2
我已经尝试了几乎所有的方法,但仍然得到以下错误: ... SWIG/_m2crypto_wrap.c:5258:12: 警告:隐式转换会丢失整数精度:从 'long' 到 'int' [-Wshorten-64-to-32] return BIO_do_handshake(bio); ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~ /usr/local/opt/openssl/include/openssl/bio.h:502:34: 注意:从宏 'BIO_do_handshake' 扩展 # define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:7640:12: 警告:隐式转换会丢失整数精度:从 'long' 到 'int' [-Wshorten-64-to-32] return BIO_ctrl(bio, BIO_C_SET_SSL, flag, (char *)ssl); ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:7699:19: 警告:隐式转换会丢失整数精度:从 'unsigned long' 到 'int' [-Wshorten-64-to-32] err = ERR_get_error(); ~ ^~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:7751:59: 警告:隐式转换会丢失整数精度:从 'long' 到 'int' [-Wshorten-64-to-32] ms = ((start->tv_sec + (int)timeout) - tv.tv_sec) * 1000; ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ SWIG/_m2crypto_wrap.c:7996:19: 警告:隐式转换会丢失整数精度:从 'unsigned long' 到 'int' [-Wshorten-64-to-32] err = ERR_get_error(); ~ ^~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:8026:37: 警告:隐式转换会丢失整数精度:从 'Py_ssize_t' (aka 'long') 到 'int' [-Wshorten-64-to-32] r = SSL_write(ssl, buf.buf, buf.len); ~~~~~~~~~ ~~~~^~~ SWIG/_m2crypto_wrap.c:8069:37: 警告:隐式转换会丢失整数精度:从 'Py_ssize_t' (aka 'long') 到 'int' [-Wshorten-64-to-32] r = SSL_write(ssl, buf.buf, buf.len); ~~~~~~~~~ ~~~~^~~ SWIG/_m2crypto_wrap.c:8087:19: 警告:隐式转换会丢失整数精度:从 'unsigned long' 到 'int' [-Wshorten-64-to-32] err = ERR_get_error(); ~ ^~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:8143:12: 警告:隐式转换会丢失整数精度:从 'long' 到 'int' [-Wshorten-64-to-32] return SSL_CTX_set_session_cache_mode(ctx, mode); ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/opt/openssl/include/openssl/ssl.h:2484:9: 注意:从宏 'SSL_CTX_set_session_cache_mode' 扩展 SSL_CTX_ctrl(ctx,SSL_CTRL_SET_SESS_CACHE_MODE,m,NULL) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:8148:12: 警告:隐式转换会丢失整数精度:从 'long' 到 'int' [-Wshorten-64-to-32] return SSL_CTX_get_session_cache_mode(ctx); ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/opt/openssl/include/openssl/ssl.h:2486:9: 注意:从宏 'SSL_CTX_get_session_cache_mode' 扩展 SSL_CTX_ctrl(ctx,SSL_CTRL_GET_SESS_CACHE_MODE,0,NULL) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SWIG/_m2crypto_wrap.c:8680:14: 警告:隐式转换会丢失
----------------------------------------

命令 "/usr/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/q5/qk1gf6z15kschmf5dwb3mqvh0000gn/T/pip-build-WyOs_y/M2crypto/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/q5/qk1gf6z15kschmf5dwb3mqvh0000gn/T/pip-tp6pQY-record/install-record.txt --single-version-externally-managed --compile" 失败,错误代码为 1,在 /private/var/folders/q5/qk1gf6z15kschmf5dwb3mqvh0000gn/T/pip-build-WyOs_y/M2crypto/

我花了很长时间才让它工作。最终的解决方法是使用sudo进行简单的chmod操作: sudo chmod 777 /Library/Python/2.7/site-packages/


你的回答无法阅读,请在阅读此文后考虑重新格式化。 - Qusai Alothman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接