首页 > 解决方案 > 如何导入在 Linux 消费计划中作为 Azure 函数运行的 Python 模块?

问题描述

我在本地运行的 Python 代码首先导入以下库:

import logging
import azure.functions as func
from sendgrid import SendGridAPIClient
from datetime import datetime
import wikipedia
import urllib.request, json

这些也列在 requirements.txt(屏幕截图)中。我的代码在本地运行得很好,但是一旦我上传到 Azure 函数,我就会收到错误(截图): ModuleNotFoundError: No module named 'sendgrid'

我在其他地方读到过,您也许可以通过 Kudu 在虚拟环境中安装这些模块。但是消费计划似乎不支持 Kudu:

工藤的截图

如何在使用 Azure Functions 的消费计划中运行带有外部模块/库的 Python 代码?

标签: pythonazureazure-functionsazure-function-app

解决方案


我通过单击 VS Code 中的“部署到功能应用程序”在我身边对其进行了测试,它成功了。

然后我通过如下所示的命令部署它:

func azure functionapp publish <APP_NAME> --build remote

部署后,它也可以正常工作。

由于我不知道您选择哪种方式将其从本地部署到 azure,如果您尝试了上述两种解决方案但都失败了,我认为您可以尝试使用以下命令:

func azure functionapp publish <APP_NAME> --build local

使用 --build local 选项,从 requirements.txt 文件中读取项目依赖项,并在本地下载并安装这些依赖包。项目文件和依赖项从本地计算机部署到 Azure。这会导致将更大的部署包上传到 Azure。我认为这个解决方案不会丢失模块“sendgrid”。


推荐阅读