在 Apple Silicon M1 Mac 上安装加密

时间:2021-02-03 20:14:52

标签: python-cryptography apple-silicon apple-m1

帮助!我正在尝试在我的 m1 上安装加密。我知道我可以在 rosetta 模式下运行终端,但我想知道是否有办法不这样做。

输出:

    ERROR: Command errored out with exit status 1:
     command: /opt/homebrew/opt/python@3.9/bin/python3.9 /opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/tmpl4sga84k
         cwd: /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-install-jko4b562/cryptography_7b1bbc9ece2f481a8e8e9ea03b1a0030
    Complete output (55 lines):
    
        =============================DEBUG ASSISTANCE=============================
        If you are seeing a compilation error please try the following steps to
        successfully install cryptography:
        1) Upgrade to the latest pip and try again. This will fix errors for most
           users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
        2) Read https://cryptography.io/en/latest/installation.html for specific
           instructions for your platform.
        3) Check our frequently asked questions for more information:
           https://cryptography.io/en/latest/faq.html
        =============================DEBUG ASSISTANCE=============================
    
    Traceback (most recent call last):
      File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module>
        main()
      File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
      File "/opt/homebrew/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 133, in prepare_metadata_for_build_wheel
        return hook(metadata_directory, config_settings)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 161, in prepare_metadata_for_build_wheel
        self.run_setup()
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 145, in run_setup
        exec(compile(code, __file__, 'exec'), locals())
      File "setup.py", line 44, in <module>
        setup(
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
        return distutils.core.setup(**attrs)
      File "/opt/homebrew/Cellar/python@3.9/3.9.1_7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/core.py", line 108, in setup
        _setup_distribution = dist = klass(attrs)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 432, in __init__
        _Distribution.__init__(self, {
      File "/opt/homebrew/Cellar/python@3.9/3.9.1_7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 292, in __init__
        self.finalize_options()
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 708, in finalize_options
        ep(self)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/setuptools/dist.py", line 715, in _finalize_setup_keywords
        ep.load()(self, ep.name, value)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 219, in cffi_modules
        add_cffi_module(dist, cffi_module)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 49, in add_cffi_module
        execfile(build_file_name, mod_vars)
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/setuptools_ext.py", line 25, in execfile
        exec(code, glob, glob)
      File "src/_cffi_src/build_openssl.py", line 77, in <module>
        ffi = build_ffi_for_binding(
      File "src/_cffi_src/utils.py", line 54, in build_ffi_for_binding
        ffi = build_ffi(
      File "src/_cffi_src/utils.py", line 74, in build_ffi
        ffi = FFI()
      File "/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/cffi/api.py", line 48, in __init__
        import _cffi_backend as backend
    ImportError: dlopen(/private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so, 2): Symbol not found: _ffi_prep_closure
      Referenced from: /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so
      Expected in: flat namespace
     in /private/var/folders/hj/5zfkv68d7lqgrfqt046bn23c0000gn/T/pip-build-env-9bqzge_f/overlay/lib/python3.9/site-packages/_cffi_backend.cpython-39-darwin.so

我尝试按照他们的说明在该代码块中说的那样构建和运行,但遇到了同样的错误。我环顾四周,似乎还没有人找到修复程序,但这些东西通常已经两个月大了。我错过了什么?

4 个答案:

答案 0 :(得分:13)

此问题是由于 libffi 标头版本与动态链接器找到的 libffi 版本不匹配造成的。一般而言,遇到此问题的用户似乎安装了自制软件 libffi,并以某种方式针对该问题构建了 Python。

发生这种情况时,cfficryptography 依赖项)会编译,但在运行时失败并引发此错误。这应该可以通过将正确的路径作为链接器参数传递来解决。要重新安装 cffi,您应该 pip uninstall cffi 后跟

LDFLAGS=-L$(brew --prefix libffi)/lib CFLAGS=-I$(brew --prefix libffi)/include pip install cffi --no-binary :all:

这是一个丑陋的解决方法,但可以让您暂时克服这个障碍。

更新:我已经为 macOS 上传了 arm64 轮子,因此如果您的 pip 是最新的,则不再需要以下编译。但是,如果由于某种原因您希望自己编译它:

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

答案 1 :(得分:2)

可能你会遇到更多封装的问题,而且每个封装都有自己的 Apple Silicon 解决方案,这很累人。

我得出了最终解决方案:使用 x86_x64 Homebrew 安装 x86 软件包,包括 Python。因此,您的所有要求都像在 x86_x64 macs 上一样安装,并且不再有编译错误等问题。

说明:

  1. 在 Rosetta 2 下运行 iTerm2(或默认终端应用程序)(右键单击应用程序图标 -> Get info -> Open using rosetta)。
  2. 像往常一样安装自制软件 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 或者出于安全原因,您可以从 https://brew.sh/ 获取此链接(切勿在未仔细检查的情况下从 stackoverflow 复制 curl 命令)。
  3. 在您的 ~/.zshrc(如果您使用 ZSH)或 ~/.bash_profile(如果您是 bash 用户)中添加别名:alias brew='arch -x86_64 /usr/local/bin/brew'
  4. 在 iTerm2 Open using rosetta 中关闭 Get info

现在,每次在终端应用程序中打印 brew 时,您都将运行 x86_x64 Homebrew。当您从 brew 安装任何软件包时,它会自动在 Rosetta 2 下运行。

答案 2 :(得分:1)

我之前无法安装 cffi,直到我发现了一个不相关的问题。我在这里待了大约两天,直到我找到了这个命令:

python3 -m ensurepip --upgrade

神奇的是,一切都开始为我工作。它来自不同来源的 Python 和 Pip 之间的问题。

从这个问题中窃取的答案:using pip3: module "importlib._bootstrap" has no attribute "SourceFileLoader"

编辑:这可能是上述海报的礼貌,因此可能无关。如果是这样,谢谢你匿名的人!

答案 3 :(得分:0)

This answer here worked like a charm! @paveldroo

作为上述答案的扩展,我继续将第 3 步中的别名保存为 alias ibrew='arch -x86_64 /usr/local/bin/brew' at ~/.zshrc

这意味着当我使用 brew 命令安装任何东西时,它会为 M1 架构安装,而当我使用 ibrew 命令安装时,它会为 -x86_64 架构安装。

因此,我在我的系统上安装了两个 python3 实例,一个在 /opt/homebrew/bin/python3 使用 brew,另一个在 /usr/local/bin/python3 使用 ibrew

这两个版本在根据需要创建项目虚拟环境方面增加了一些灵活性。例如,您可以使用以下方法创建虚拟环境:

  1. /usr/local/bin/python3 -m venv venv 用于 -x86_64 架构
  2. /opt/homebrew/bin/python3 -m venv venv 用于 M1 架构