Azure Functions 部署失败,因为 pip install 找不到环境变量/应用程序设置

Azure Functions deployment fails because pip install cannot find environment variable / app setting

编辑: 我在部署到 Azure 时的缺陷是我没有保存我引入的新应用程序设置 facepalm。保存 PIP_EXTRA_INDEX_URL=https://<token>@raw.githubusercontent.com/<gituser> 允许 pip 最终安装我的私有库。

然而,在 VSCode 上使用 F5 进行本地调试时仍然无法正常工作,无论我的 local.settings.json 是什么。下面我将描述让它在本地工作的技巧,但我更愿意解决为什么它在我的本地设置中不起作用。

我的requirements.txt表示私有库为<package_name> @ https://raw.githubusercontent.com/<gituser>/<repo>/<branch>/dist/<package_name>0.1.11-py3-none-any.whl。基于在 Azure 中添加 PIP_EXTRA_INDEX_URL 允许部署这一事实,我试图在我的 local.settings.json:

中模仿它
{
  "IsEncrypted": false,
  "Values": {
    ...
    "PIP_EXTRA_INDEX_URL": "https://<token>@raw.githubusercontent.com/<gituser>",
    ...
  }
}

但是,当 Azure 的 pip 尝试从 requirements.txt 安装时,我收到 404 错误。

本地,在 tasks.json 中,如果我在 运行 pip 安装之前设置了环境变量,F5 开始工作:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "command": "host start",
            "problemMatcher": "$func-python-watch",
            "isBackground": true,
            "dependsOn": "pip install (functions)"
        },
        {
            "label": "pip install (functions)",
            "type": "shell",
            "osx": {
            ...
            "windows": {
                "command": "$env:PIP_EXTRA_INDEX_URL = 'https://<token>@raw.githubusercontent.com/<gituser>'; ${config:azureFunctions.pythonVenv}\Scripts\python -m pip install -r requirements.txt"
            },
            ...
        }
    ]
}

换句话说,我的 F5 没有在 pip install 任务 运行 之前从 local.settings.json 读取环境变量(请注意,函数本身正确接收环境变量作为由成功的 os.environ[] 调用证明)。另请注意,如果我在 tasks.json 中设置环境变量,它在本地确实有效,这并不理想。我更喜欢 local.settings.json 在 Azure 上镜像我的应用程序设置。

在此 issue 中,已确认这是预期行为,我的解决方法是有效的:

"local.settings.json" is unique to core tools and thus only applies during the func start command. Since "pip install" happen before func start, you would have to set the env var elsewhere (and yes tasks.json is a good place).