gpt4 book ai didi

python - 将模块导入 Python Azure Function

转载 作者:行者123 更新时间:2023-12-01 07:35:43 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Azure 函数,它接收 HTTP 事件,获取该请求正文中的 JSON 对象,并将该对象保存为 CosmosDB 集合中的文档。然而,我似乎对导入 python 模块很着迷。

__init__.py

import json
import azure.functions as func
import azure.cosmos.cosmos_client as cosmos_client


config = {
'ENDPOINT': 'https://XXXXXX.documents.azure.com:443/',
'PRIMARYKEY': 'XXXXXX',
'DATABASE': 'my-database',
'CONTAINER': 'my-container'
}

def main(req: func.HttpRequest) -> func.HttpResponse:
try:
client = cosmos_client.CosmosClient(config['ENDPOINT'], {'masterKey': config['PRIMARYKEY']})
results = req.get_json()

client.get_database_client(config['DATABASE']).get_container_client(config['CONTAINER']).upsert_item(results)

return func.HttpResponse("Success", status_code=200)
except ValueError:
return func.HttpResponse("Please pass a JSON object", status_code=400)

错误日志

019-07-11T22:51:22.613140092Z: [INFO]  Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.save-to-cosmos ---> Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
2019-07-11T22:51:22.616516971Z: [INFO] Exception: ModuleNotFoundError: No module named 'azure.cosmos'
2019-07-11T22:51:22.616547173Z: [INFO] Stack: File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 230, in _handle__function_load_request
2019-07-11T22:51:22.632613023Z: [INFO] func_request.metadata.entry_point)
2019-07-11T22:51:22.632631824Z: [INFO] File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/loader.py", line 66, in load_function
2019-07-11T22:51:22.632637524Z: [INFO] mod = importlib.import_module(fullmodname)
2019-07-11T22:51:22.632641725Z: [INFO] File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
2019-07-11T22:51:22.632646025Z: [INFO] return _bootstrap._gcd_import(name[level:], package, level)
2019-07-11T22:51:22.632650325Z: [INFO] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2019-07-11T22:51:22.632655025Z: [INFO] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2019-07-11T22:51:22.632660926Z: [INFO] File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2019-07-11T22:51:22.632682827Z: [INFO] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2019-07-11T22:51:22.632688427Z: [INFO] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2019-07-11T22:51:22.632692727Z: [INFO] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2019-07-11T22:51:22.632697128Z: [INFO] File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2019-07-11T22:51:22.632723529Z: [INFO] File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2019-07-11T22:51:22.632729029Z: [INFO] File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2019-07-11T22:51:22.632733329Z: [INFO] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2019-07-11T22:51:22.632737430Z: [INFO] File "/home/site/wwwroot/save-to-cosmos/__init__.py", line 3, in <module>
2019-07-11T22:51:22.632741830Z: [INFO] import azure.cosmos.cosmos_client as cosmos_client

我尝试过像这个问题建议的那样浏览 Kudu Importing Python modules for Azure Function并尝试运行 pip install azure-cosmos 但这似乎并不能解决问题。

最佳答案

您需要在 Function App KUDU 中添加 python 扩展。

第 1 步:安装自定义 Python 版本

导航到 Kudu 控制台 - https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole在 kudu cli 中运行以下命令将 Python 安装在 D:\home\site\tools 文件夹中

nuget.exe安装-源https://www.siteextensions.net/api/v2/ -输出目录 D:\home\site\tools python352x64

第2步:我们需要在D:\home\site\tools文件夹中有python.exe。所以让我们移动子文件夹内容(D:\home\site\tools\python352x64.3.5.2.6\content\Python35) 使用以下命令到 D:\home\site\tools

mv /d/home/site/tools/python352x64.3.5.2.6/content/python35/* /d/home/site/tools/

第三步:安装自定义Python包。

导航到 Kudu 控制台 - https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole运行以下命令在函数应用程序中安装 pandas 模块:

D:\home\site\tools\python.exe -m pip install azure-cosmos

测试示例:

我安装了 requests 包来进行一些测试。

 D:\home\site\tools\python.exe -m pip install requests

enter image description here

Python代码:

enter image description here

输出:

enter image description here

关于python - 将模块导入 Python Azure Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56998307/

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