0

我有一个 azure 函数应用程序,可以在消息发送到服务总线时触发。代码逻辑工作得很好,我可以手动向服务总线发送一条消息,运行我的 python 代码来接收该消息,它工作得很好。

但是当我将此逻辑集成到 azure 函数应用程序中时,我收到以下错误:

Result: Failure Exception: ImportError: cannot import name 'c_uamqp' from partially initialized module 'uamqp' (most likely due to a circular import) (/home/site/wwwroot/.python_packages/lib/site-packages/uamqp/__init__.py) Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 402, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 606, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/SentimentAnalysis/testServiceBus.py", line 10, in main from azure.servicebus import ServiceBusClient File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/servicebus/__init__.py", line 6, in <module> from uamqp import constants File "/home/site/wwwroot/.python_packages/lib/site-packages/uamqp/__init__.py", line 12, in <module> from uamqp import c_uamqp # pylint: disable=import-self

根据第一行,存在循环导入错误,导致函数应用失败。

我的代码如下:

import logging
import azure.functions as func
import json
import boto3
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
from azure.servicebus import ServiceBusClient, ServiceBusMessage




def main(message: func.ServiceBusMessage):

我认为这种冲突发生在ServiceBusMessage两次导入时,但我不确定。

我在想如果我摆脱 azure.servicebus 并使用azure.functions应该可以解决问题。但是检查这个库,我找不到任何方法来初始化ServiceBusClient.

是否有任何人可以帮助我了解问题出在哪里以及如何解决?

非常感谢您的帮助

4

1 回答 1

0

当存在 uamqp 项目的本地副本并且将其路径附加到 PYTHONPATH 时会遇到此类错误。

from azure.servicebus import ServiceBusClient,
ServiceBusMessage def  main(message: func.ServiceBusMessage): 
sb_client = ServiceBusClient.from_connection_string(...)

但我知道 ServiceBusService 在 v7.0.0 中已弃用 https://pypi.org/project/azure-servicebus/7.0.0/

from azure.servicebus import ServiceBusClient, ServiceBusMessage

Azure SDK 更新经常发生并引入了重大更改:0.2 -> 0.5:

# From azure.servicebus import ServiceBusService <- This will now raise an ImportError
from azure.servicebus.control_client import ServiceBusService

0.5 -> 7.0.0

#from azure.servicebus import ServiceBusService <-- Now outdated
from azure.servicebus.management import ServiceBusAdministrationClient 

根据上述指南和您的版本兼容性使用导入语句。

然而,这还不够:请务必检查此迁移指南中的进一步 API 更改: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/servicebus/azure-servicebus/migration_guide。 MD

于 2021-11-02T07:00:59.237 回答