简体   繁体   English

无法从 Azure 函数中的 azure.storage.blob 导入 AppendBlobService

[英]Unable to import AppendBlobService from azure.storage.blob in Azure function

I am using the below azure function which takes http trigger as input.我正在使用以下 azure 函数,它将 http 触发器作为输入。

import logging
import azure.functions as func
from . import test

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    strng = req.params.get('strng')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.sum = {test.testfunc(strng)}")
    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
        )

Below is the test.py file which I imported in the init .py.下面是我在init .py 中导入的 test.py 文件。

import json
import pandas as pd
from pandas import DataFrame
from azure.storage.blob import AppendBlobService
from datetime import datetime


def testfunc(strng):
    # return strng
    API = json.loads(strng)

    test = pd.json_normalize(parse, record_path='Vol', meta=['studyDate'])
    df = pd.DataFrame(test)
    df["x"] = df["Vol"] * 2
    df["y"] = df["Vol"] * 50
    df1 = df[['Date', 'x', 'y']]
    df2 = df1.to_json(orient='records')
    append_blob_service = AppendBlobService(account_name='actname',
                                         account_key='key')
    date = datetime.now()
    blobname = f"test_cal{date}.json"
    append_blob_service.create_blob('container', blobname, if_none_match="*")
    append_blob_service.append_blob_from_text('container', blobname, text=df2)
    return df2
    

The above function works when I run it in pycharm and databricks .当我在 pycharm 和 databricks 中运行上面的函数时,它可以工作。 But when I run the Azure function via visuals studio code, I get the below error.但是,当我通过 Visuals Studio 代码运行 Azure 函数时,出现以下错误。

Exception: ImportError: cannot import name 'AppendBlobService' from 'azure.storage.blob'

I have tried below and still get the same error.我在下面尝试过,仍然得到同样的错误。

pip install azure-storage --upgrade
pip install azure-storage-blob

Kindly provide assistance with the error.请提供有关错误的帮助。

Is there any other way I can save the df2 variable to Azure storage.有没有其他方法可以将 df2 变量保存到 Azure 存储。

Thank you.谢谢你。

According to Document it says,根据文档,它说,

If the module (for example, azure-storage-blob ) cannot be found, Python throws the ModuleNotFoundError.如果找不到模块(例如azure-storage-blob ),Python 会抛出ModuleNotFoundError. If it is found, there may be an issue with loading the module or some of its files.如果找到,则加载模块或其某些文件可能存在问题。 Python would throw a ImportError in those cases.在这些情况下,Python 会抛出ImportError

  • Try setting python to environment variable FUNCTIONS_WORKER_RUNTIME or尝试将python设置为环境变量FUNCTIONS_WORKER_RUNTIME
  • Try adding azure.core to your requirements.txt file.尝试将azure.core添加到您的requirements.txt文件中。

Taken References from:引用自:

ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob' ImportError:无法从“azure.storage.blob”导入名称“BlockBlobService”

Azure Functions fails running when deployed Azure Functions 部署时运行失败

The current version library contains the blobserviceclient instead of the blockblockblockservice.当前版本库包含 blobserviceclient 而不是 blockblockblockservice。 This works for me by using version 2.1.0 can solve it:这对我有用,使用 2.1.0 版可以解决它:

pip install azure-storage-blob==2.1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 azure.storage.blob 导入名称 BlockBlobService - Cannot import name BlockBlobService from azure.storage.blob ImportError:无法从“azure.storage.blob”导入名称“BlockBlobService” - ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob' Docker:无法从“azure.storage.blob”导入名称“BlobServiceClient” - Docker: cannot import name 'BlobServiceClient' from 'azure.storage.blob 尝试使用 Python 库 azure-storage-blob 时未解决的导入“azure.storage.blob” - Unresolved import 'azure.storage.blob' when trying to use Python library azure-storage-blob 从“azure.storage.blob”导入“BlobServiceClient”时出错 - error importing 'BlobServiceClient' from 'azure.storage.blob' ImportError:没有名为azure.storage.blob的模块 - ImportError: No module named azure.storage.blob 从 SQL Server 代理运行代码时出现问题 - azure.storage.blob - 模块 azure 未找到 - Issue when running code from SQL server agent - azure.storage.blob - module azure not found 导入azure.storage.blob时出现xml.etree错误 - xml.etree error when importing azure.storage.blob 如何为 python 安装 azure.storage.blob 库? - How to install azure.storage.blob library for python? Using azure.storage.blob to write Python DataFrame as CSV into Azure Blob - Using azure.storage.blob to write Python DataFrame as CSV into Azure Blob
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM