ModuleNotFoundError:没有名为“azure.identity”的模块

问题描述 投票:0回答:1

我正在尝试使用 Python 在 Visual Studio Code 中编写 Webhook 侦听器。我承认我是个新手,所以请耐心等待。 VSC 中 Webhook 侦听器的初始示例非常简单。我使用 func host start 在终端窗口中本地运行它,然后使用 VSC 中的 Postman 进行测试,并获得预期的响应和 200 状态。

但我想做的不仅仅是简单地给出响应/200,我想抓取传入的 JSON 数据,将其存储在 Azure Data Lake 中,然后给出响应。为此,我必须添加一些库。我的 function_app.py 文件中的导入已从

import logging

import azure.functions as func

到此

import logging
import datetime
import os
import json

import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
from azure.storage.filedatalake import DataLakeServiceClient

我更新了我的requirements.txt 文件,以便它从

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions

到此

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions
azure-storage-file-datalake
azure-identity
azure-keyvault
requests

我的 local.settings.json 现在包含 KEY_VAULT_URL、STORAGE_ACCOUNT_NAME 等条目。

以为一切都已设置,我使用 func host start 在终端窗口中启动代码,现在得到一个错误,可归结为

ModuleNotFoundError: No module named 'azure.identity'

我似乎没有告诉 VSC azure.identity 存在的位置。它在

.venv\Lib\site-packages\azure_identity-1.16.0.dist-info

我知道我可能错过了对其他人来说非常明显的东西,但我将不胜感激任何建议。

我尝试过使用 ChatGPT、GitHub Copilot 等进行研究,但似乎没有什么效果。

我已经执行了各个软件包的 pip 安装,我已经完成了 pip install -rrequirements.txt 以确保一切可用。老实说我很茫然。

python azure-functions webhooks
1个回答
0
投票

当您使用虚拟环境时。您需要在虚拟环境中安装

requirements.txt
文件。

要激活虚拟环境,请按照以下命令操作

.venv\Scripts\activate  #for Windows ()
.venv\bin\activate      #for Linux

#then run your command to install packages
pip install -r requirements.txt

#then run your func host start command
func host start

您将在前面看到虚拟环境标签

(.venv)
,如下所示。

这对我有用。

import azure.functions as func
import logging
import os
from azure.identity import DefaultAzureCredential
from azure.storage.filedatalake import DataLakeServiceClient


app = func.FunctionApp()

def get_service_client():
    account_name = os.getenv("STORAGE_ACCOUNT_NAME")
    credential = DefaultAzureCredential()
    service_client = DataLakeServiceClient(
        account_url=f"https://{account_name}.blob.core.windows.net",
        credential=credential
    )
    return service_client

def read_file_from_datalake(service_client, filesystem_name, file_path):
    file_system_client = service_client.get_file_system_client(file_system=filesystem_name)
    file_client = file_system_client.get_file_client(file_path)
    download = file_client.download_file()
    downloaded_bytes = download.readall()
    return downloaded_bytes.decode('utf-8')

@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    source_filesystem = "func"
    source_filepath = "input.txt"

    try:
        service_client = get_service_client()

        # Read file content from the source
        file_content = read_file_from_datalake(service_client, source_filesystem, source_filepath)
        logging.info(f"Read content from {source_filepath}: {file_content}")
        processed_content = file_content.upper()

        return func.HttpResponse(f"File content in Uppercase{processed_content}")

    except Exception as e:
        logging.error(f"Error processing file: {e}")
        return func.HttpResponse(f"Error processing file: {e}", status_code=500)

requirements.txt
:

azure-functions
azure-identity
azure-storage-file-datalake

OUTPUT

© www.soinside.com 2019 - 2024. All rights reserved.