Azure 函数未在 M1 上运行

zin*_*rim 5 python azure-functions apple-silicon

跑步

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
        )

Run Code Online (Sandbox Code Playgroud)

在 vscode 中


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. 
Run Code Online (Sandbox Code Playgroud)

我尝试了很多事情,包括:

  • 使用支持的 python 版本创建 conda 环境
  • 进入项目根目录,删除.venv文件夹
  • 激活新创建的conda环境
  • 使用创建新的虚拟环境python3 -m venv .venv/

我还尝试应用此处概述的 Rosetta 修复 程序How to run the Homebrew installer under Rosetta 2 on M1 Macbook

但没有成功。我怎样才能解决这个问题?

Dan*_*der 5

我在 M1 Mac 上不断遇到 python 问题,直到我完全使用命令行上的 Rosetta。为此,我做了以下事情:

\n
    \n
  1. 更新 Rosetta:\n在终端类型中:
  2. \n
\n
softwareupdate --install-rosetta\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 在 Finder 中,键入\xe2\x87\xa7\xe2\x8c\x98G并转到 /Applications/Utilities。然后复制终端:
  2. \n
\n

在此输入图像描述

\n
    \n
  1. 将第二个终端重命名为“Rosetta”(或任何您喜欢的名称),并通过在“获取信息”对话框中选中“使用 Rosetta 打开”来在 Rosetta 中执行它:
  2. \n
\n

在此输入图像描述

\n
    \n
  1. 打开 Rosetta 终端并确保它i386在您发出命令时显示arch
  2. \n
\n

在此输入图像描述

\n
    \n
  1. 在该终端中,安装 homebrew(根据 homebrew 主页):
  2. \n
\n
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 安装 homebrew 后,使用 homebrew 安装 miniconda:
  2. \n
\n
brew install --cask miniconda\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 创建一个 conda 环境,例如这里的 python 3.9 env 名为azure
  2. \n
\n
conda create -n azure python=3.9\n
Run Code Online (Sandbox Code Playgroud)\n

然后激活环境:

\n
conda activate azure\n
Run Code Online (Sandbox Code Playgroud)\n

从现在开始,您就拥有了一个功能齐全的 i386 Python 系统。这解决了我在 M1 Mac 上使用 Azure、Numpy、Pandas 等时遇到的所有问题。

\n