M1芯片设备上Azure Function无法运行的问题求助
解决Azure Functions在Arm64架构下运行Python函数的"Architecture Arm64 is not supported"错误
问题描述
用户在VS Code中运行以下Python编写的Azure HTTP触发函数:
import logging import azure.functions as func def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.") else: return func.HttpResponse( "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.", status_code=200 )
执行func host start命令时出现错误,核心提示为**"Architecture Arm64 is not supported for language python"**,完整错误日志如下:
pyenv shell 3.9.12 Requirement already satisfied: azure-functions in ./.venv/lib/python3.9/site-packages (from -r requirements.txt (line 5)) (1.12.0) WARNING: You are using pip version 21.2.4; however, version 22.3 is available. You should consider upgrading via the '/Users/deniz/Library/CloudStorage/OneDrive-DualCitizen/Dev/2ndfunction/.venv/bin/python -m pip install --upgrade pip' command. * Terminal will be reused by tasks, press any key to close it. * Executing task: . .venv/bin/activate && func host start Found Python version 3.9.10 (python3). Azure Functions Core Tools Core Tools Version: 4.0.4829 Commit hash: N/A (64-bit) Function Runtime Version: 4.11.2.19273 [2022-10-23T09:38:17.290Z] Failed to initialize worker provider for: /opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4829/workers/python [2022-10-23T09:38:17.290Z] Microsoft.Azure.WebJobs.Script: Architecture Arm64 is not supported for language python. [2022-10-23T09:38:17.818Z] Failed to initialize worker provider for: /opt/homebrew/Cellar/azure-functions-core-tools@4/4.0.4829/workers/python [2022-10-23T09:38:17.818Z] Microsoft.Azure.WebJobs.Script: Architecture Arm64 is not supported for language python. [2022-10-23T09:38:17.957Z] A host error has occurred during startup operation '29ac434c-0276-4c0b-a85f-c9e4863577dc'. [2022-10-23T09:38:17.957Z] Microsoft.Azure.WebJobs.Script: Did not find functions with language [python]. [2022-10-23T09:38:17.964Z] Failed to stop host instance '3d492f60-0fd3-48dd-bce0-d7cc99da71c8'. [2022-10-23T09:38:17.964Z] Microsoft.Azure.WebJobs.Host: The host has not yet started. Value cannot be null. (Parameter 'provider') [2022-10-23T09:38:17.986Z] A host error has occurred during startup operation '1327f3e2-25e9-4318-bfea-0bac013aff02'. [2022-10-23T09:38:17.986Z] Microsoft.Extensions.DependencyInjection: Cannot access a disposed object. [2022-10-23T09:38:17.986Z] Object name: 'IServiceProvider'. * Terminal will be reused by tasks, press any key to close it.
已尝试的无效方法
- 创建兼容Python版本的conda环境
- 重建虚拟环境
- 应用Rosetta 2修复
可行解决方案
方法1:全链路强制使用x86_64架构运行
- 配置终端以Rosetta模式运行:
- 找到Mac上的终端应用,右键选择「显示简介」,勾选「使用Rosetta打开」,重启终端。
- 安装x86版本的Azure Functions Core Tools:
# 先卸载现有Arm64版本(如果有) brew uninstall azure-functions-core-tools@4 # 用x86架构的Homebrew安装 arch -x86_64 /usr/local/bin/brew install azure-functions-core-tools@4 - 创建x86架构的Python虚拟环境:
# 用pyenv安装x86版本的Python 3.9.12 arch -x86_64 pyenv install 3.9.12 arch -x86_64 pyenv local 3.9.12 # 创建虚拟环境 arch -x86_64 python -m venv .venv # 激活环境并安装依赖 source .venv/bin/activate pip install -r requirements.txt - 运行函数:
func host start
方法2:启用Arm64 Python Worker支持
如果你的Azure Functions Core Tools版本是4.15.0及以上,可以通过配置启用Arm64支持:
- 在函数项目根目录的
local.settings.json中添加以下配置(如果文件不存在则新建):{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "python", "AzureWebJobsStorage": "", "PYTHON_WORKER_ARM64_ENABLED": "1" } } - 确保你的Python环境是Arm64版本,然后重新运行
func host start。
内容的提问来源于stack exchange,提问作者Zin Yosrim
相关产品推荐
相关产品推荐

