在Big Sur操作系统上安装cryptography时出现Pip错误

12

使用pip版本20.2.4,Python 3.8.2和Big Sur 11.0.1。

当我运行pip install cryptography时,会出现以下错误:

  Building wheels for collected packages: cffi
    Building wheel for cffi (setup.py): started
    Building wheel for cffi (setup.py): finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /Users/xxx/projects/xxx/venv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/setup.py'"'"'; __file__='"'"'/private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-wheel-asotxqfl
         cwd: /private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/
    Complete output (42 lines):
    running bdist_wheel
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.14.6-x86_64-3.8
    creating build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/backend_ctypes.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/error.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/setuptools_ext.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/__init__.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/cffi_opcode.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/vengine_gen.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/pkgconfig.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/model.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/ffiplatform.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/api.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/vengine_cpy.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/commontypes.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/lock.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/recompiler.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/cparser.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/verifier.py -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/_cffi_include.h -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/parse_c_type.h -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/_embedding.h -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    copying cffi/_cffi_errors.h -> build/lib.macosx-10.14.6-x86_64-3.8/cffi
    running build_ext
    building '_cffi_backend' extension
    creating build/temp.macosx-10.14.6-x86_64-3.8
    creating build/temp.macosx-10.14.6-x86_64-3.8/c
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/usr/local/Cellar/libffi/3.3/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ffi -I/Users/xxxx/projects/xxxx/venv/include -I/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c c/_cffi_backend.c -o build/temp.macosx-10.14.6-x86_64-3.8/c/_cffi_backend.o
    c/_cffi_backend.c:5854:2: error: Apple Arm64 ABI requires ffi_prep_cif_var
    #error Apple Arm64 ABI requires ffi_prep_cif_var
     ^
    c/_cffi_backend.c:6304:9: warning: 'ffi_prep_closure' is deprecated [-Wdeprecated-declarations]
        if (ffi_prep_closure(closure, &cif_descr->cif,
            ^
    /usr/local/Cellar/libffi/3.3/include/ffi.h:341:18: note: 'ffi_prep_closure' has been explicitly marked deprecated here
      __attribute__((deprecated))
                     ^
    1 warning and 1 error generated.
    error: command 'clang' failed with exit status 1
    ----------------------------------------
    ERROR: Failed building wheel for cffi

我在升级了MacOS之前从未遇到过这种情况。有什么建议吗?


1
自最新版本以来,在cffi中有些修复,看起来很相关。你可以尝试pip install hg+https://foss.heptapod.net/pypy/cffi吗? - Armin Rigo
1
有趣。我在执行命令hg clone --noupdate -q https://foss.heptapod.net/pypy/cffi /private/var/folders/g8/nj61c9fhhbjdvbhhbmqqb00000gn/T/pip-req-build-e3dcg2vf时,出现了ERROR: Error [Errno 2] No such file or directory: 'hg'的错误。 错误提示为ERROR: Cannot find command 'hg' - do you have 'hg' installed and in your PATH?,请问您是否已经安装并将'hg'添加到PATH中? - MDalt
我能够按照建议从最新提交中安装cffi(并且导入模块有效),但是当我尝试安装cryptographyargon2-cffi时,pip 显然会尝试重新安装cffi,这当然会失败。这是在一个全新的虚拟环境中。有什么想法吗? - Julian Panetta
它声称正在尝试安装cffi,即使我使用了--no-cache-dir标志,也会出现“Using cached cffi-1.14.3.tar.gz”。 - Julian Panetta
1
实际上,这似乎是由 Big Sur 上 pip 中的错误引起的:https://github.com/pypa/pip/issues/9138 https://github.com/nolze/msoffcrypto-tool/issues/54 - Julian Panetta
显示剩余2条评论
5个回答

23

首先安装openssl

brew install openssl

然后导出编译器选项:

export CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS="1" 
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

现在你可以使用pip install了

pip3 install cryptography==2.8

2
在Big Sur 11.1上,执行"brew install openssl"命令会提示已经安装,但是我进行了导出操作并执行了"pip install"命令,感谢您的提示。 - tacan
1
@tacan 是的,但是导出编译器选项就可以解决问题了! - Lars Bilke
在Big Sur上,export行不足。尽管openssl已经安装,但我重新安装它以查看是否有帮助。对于旧版本(1.11.5)的cffi仍然失败。 - hamx0r

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

1
这应该是最佳答案!在安装有Big Sur 11.6的M1 Arm Mac上仍然是正确的。 - Bernd Verst
在我的系统上,brew install openssl 给了我 openssl@3。需要运行 brew install openssl@1.1,然后这个就可以工作了。 - J. Kevin Corcoran
这对我在搭载Big Sur 11.6的M1 Arm Mac上运行良好。我正在使用Python 2.7 pip 19.3和cryptography 3.1.1。 - pass-by-ref
这在 M1 操作系统的 pip 上可以工作,但仍无法在 virtualenv Python 3.8.9 中安装 cryptography。在操作系统 pip 上安装的 cryptography 版本是 38.0.1。 - Tan Hoo Chuan Nickson

3

@serge-g的答案对于在M1 Mac / ARM64上使用Homebrew和Big Sur 11.6的情况表现良好。

我不得不做出以下更改:

brew install openssl

export CRYPTOGRAPHY_SUPPRESS_LINK_FLAGS="1" 
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

在那时,通过pip3 install cryptography==2.8安装对我有效。

值得一提的是,在我的情况下,是Azure CLI Python包 (pip3 install azure-cli) 的依赖项。感谢@serge-g指引我找到正确的方法!


@Simeon Aleksov的答案是最好的答案,因为它可以在homebrew安装的任何位置都能正常工作! - Bernd Verst

2
根据密码学GitHub上的这个问题,截至2020年11月,仅将pip升级到最新版本即可使其正常工作。我自己在pip 19上遇到了这个错误,升级到pip 21.0.1后,问题得到了解决。

0
你应该尝试使用 poetry shellpip install cryptography

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