0

I have an azure function app to trigger in event of a message sent to service bus. The code logic works just fine and I can manually send a message to the service bus, run my python code to receive that message and it works just fine.

But when I integrate this logic into a azure function app, I get the following error:

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

According to the first line, there is a circular import error, which cause the function app to fail.

my code is as follow:

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):

I presume that this conflict happen on ServiceBusMessage that get imported twice, but I am not sure.

I was thinking that probably if I get rid of the azure.servicebus, and use the azure.functions should solve the issue. But checking this library, I couldn't find any method to initialise a ServiceBusClient.

Did this ever happened to anyone that can help me to understand where is the issue and how can I possibly solve it?

Thank you very much for your help

Nayden Van
  • 1,133
  • 1
  • 23
  • 70
  • You can refer to a similar open issue on GitHub: [Azure Function Service Bus ImportError: cannot import name 'c_uamqp' from partially initialized module 'uamqp'](https://github.com/Azure/azure-uamqp-python/issues/281) – Ecstasy Oct 18 '21 at 07:45

1 Answers1

0

This type of error is encountered when there's a local copy of the uamqp project, and the path to which is appended to the PYTHONPATH.

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

But I came to know that ServiceBusService is deprecated in v7.0.0 https://pypi.org/project/azure-servicebus/7.0.0/

from azure.servicebus import ServiceBusClient, ServiceBusMessage

Azure SDK updates often does and has introduced breaking changes: 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 

Use the import statements based on above guide and your version compatibility.

This is however not enough: Make sure to check further API changes in this migration guide: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/servicebus/azure-servicebus/migration_guide.md