本地托管 python azure 函数因 M1 而失败

Local hosting python azure function fail with M1

如标题所示,我想在本地使用 VSCode 托管 azure 函数,但出现错误。

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

local.setting.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": ""
  }
}

错误信息:

Functions:

        HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1

For detailed output, run func with --verbose flag.
....
[2022-05-09T06:52:10.300Z]     from . import dispatcher
[2022-05-09T06:52:10.300Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4483/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 19, in <module>
[2022-05-09T06:52:10.300Z]     import grpc
[2022-05-09T06:52:10.300Z]   File "/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4483/workers/python/3.9/OSX/X64/grpc/__init__.py", line 23, in <module>
[2022-05-09T06:52:10.300Z]     from grpc._cython import cygrpc as _cygrpc
[2022-05-09T06:52:10.300Z] ImportError: dlopen(/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4483/workers/python/3.9/OSX/X64/grpc/_cython/cygrpc.cpython-39-darwin.so, 0x0002): tried: '/opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4483/workers/python/3.9/OSX/X64/grpc/_cython/cygrpc.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/cygrpc.cpython-39-darwin.so' (no such file), '/usr/lib/cygrpc.cpython-39-darwin.so' (no such file)
[2022-05-09T06:52:13.512Z] Host lock lease acquired by instance ID '0000000000000000000000008F1C7F2E'.

从我们这边重现后,我们观察到如果您有 arm64 Python,它将永远无法加载 x86_64 共享库,因此我们需要启用 Rosetta,它工作在按进程级别处理。

要遵循的步骤

  1. 检查 iTerm 中的 Rosetta。
  2. 安装 homebrew、azure functions 核心工具,并在当前 homebrew 中 python。
  3. 然后 运行 你的 azure 函数。

参考资料: Support running on M1 Macs [Python]