gpt4 book ai didi

python - Python 中的 Azure 函数无法使用 pymongo

转载 作者:行者123 更新时间:2023-12-02 06:54:55 26 4
gpt4 key购买 nike

我有一个在 AKS 中运行的 MongoDB 和一个为将其公开到 AKS 集群之外而创建的负载均衡器。我还使用以下命令在 mongod.conf 中设置了bindIP

sed "s,\\(^[[:blank:]]*bindIp:\\) .*,\\1 0.0.0.0," /etc/mongod.conf
mongod --config C:\mongodb\conf\mongodb.conf

我有一个用 python 编写的 Azure 函数来连接到数据库。

我是 python 新手,只是尝试连接到 mongodb 的基本代码。

import logging

import azure.functions as func
import pymongo


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:
elif name:
uri = "mongodb://User:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="efba9c8a9ddfdfdeafdddfc1d7dec1dcdbc1dedfda" rel="noreferrer noopener nofollow">[email protected]</a>:27017/testdb"
client = pymongo.MongoClient(uri)

db = client.test
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
)

当我尝试运行代码时,出现以下错误,感谢任何帮助。

Result: Failure Exception: ModuleNotFoundError: No module named 'pymongo'. 
Troubleshooting Guide: https://aka.ms/functions-modulenotfound
Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py",
line 315, in _handle__function_load_request func = loader.load_function(
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py",
line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py",
line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py",
line 85, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py",
line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/HttpTrigger1/__init__.py",
line 4, in <module> import pymongo

最佳答案

该错误可能与多种原因相关,如下所述:

  1. 如何部署(VSCode、Azure Devops)是部署代码的requirements.txt部分?
  2. pymongo 与您的 azure 函数的 python 版本兼容吗?根据此https://pypi.org/project/pymongo/3.9.0/它是兼容的.
  3. 该函数已将 WEBSITE_RUN_FROM_PACKAGE 设置为 1?

您需要排除故障并查看此处的指南: https://aka.ms/functions-modulenotfound

关于python - Python 中的 Azure 函数无法使用 pymongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73275381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com