【问题标题】:How do I configure my Azure Function's environment so that I can use the CosmosDB client in Python code?如何配置我的 Azure Functions 环境,以便可以在 Python 代码中使用 Cosmos DB 客户端?
【发布时间】:2022-01-18 08:39:45
【问题描述】:

我正在使用 Python 编写的 API 构建 Azure 静态应用程序。我想使用 Cosmos 客户端。我能够配置我的本地机器以便本地开发工作:

  • 我将 Azure Cosmos 安装到 Python 虚拟环境中:pip install azure-cosmos
  • 在 Python 代码中,我创建并使用客户端
from azure.cosmos import cosmos_client

cosmos_client.CosmosClient(...)
  • 如果我从 Python 虚拟环境中启动 swa,一切正常:(.venv) $ swa start ...
  • 我已将 Cosmos 密钥添加到应用的配置中。

但我无法让此代码在生产中工作。我部署了代码,但功能失败。当我检查 Application Insights 时,我看到了错误消息:

执行函数时出现异常:Functions.my_commands 结果: 失败异常:ModuleNotFoundError:没有名为“azure.cosmos”的模块。

我假设我必须运行一个命令或向requirements.txt 添加一些东西,但我不知道它是什么。

【问题讨论】:

  • 在您的requirements.txt 中添加了这两个azure-functions azure.cosmosdb=xx.xx
  • 您好@RyeGrain,如果您使用此cmd 创建了工件,请告诉我们:- pip install --target="<PROJECT_DIR>/.python_packages/lib/site-packages" -r requirements.txt 并尝试部署和测试
  • 修复了它。我添加了azure-cosmos==4.2.0。请输入此内容,我会将其标记为已接受的答案。
  • 很高兴听到您的问题得到解决!!谢谢:)

标签: python azure azure-functions azure-cosmosdb azure-static-web-app


【解决方案1】:

我假设我必须运行一个命令或添加一些东西到 requirements.txt,但我不知道它是什么。

要解决此问题,请确保您已在 requirements.txt 文件中添加以下内容

azure-functions 
azure-cosmos==x.x.x 

有关更多信息,请参阅此GitHub 问题和SO THREAD

【讨论】:

  • 对于像我这样的新手来说,重要的教训是requirements.txt 中的行与您提供给pip 的行匹配。
猜你喜欢
  • 2019-10-15
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2020-08-15
  • 1970-01-01
  • 2016-04-03
相关资源
最近更新 更多