Skip to content
Advertisement

ImportError: cannot import name ‘ServiceBusClient’ from ‘azure.servicebus’

I wanna run a simple Azure service bus program, I received the below error:

ImportError: cannot import name 'ServiceBusMessage' from 'azure.servicebus'

As I search on the internet, I should install Azure service bus and also I installed. My Python version: 3.8.9 My pip freeze:

azure-common==1.1.26
azure-core==1.13.0
azure-functions==1.6.0
azure-nspkg==3.0.2
azure-servicebus==7.1.1
azure-storage==0.36.0

My code:

from azure.servicebus import ServiceBusClient, ServiceBusMessage

import os
connstr = os.environ['******']
queue_name = os.environ['*****']

with ServiceBusClient.from_connection_string(connstr) as client:
    with client.get_queue_sender(queue_name) as sender:
        single_message = ServiceBusMessage("Single Message")
        sender.send_messages(single_message)

        messages = [ServiceBusMessage(
            "First Message"), ServiceBusMessage("Second message")]
        sender.send_messages(messages)

Advertisement

Answer

This problem is caused by incompatible versions. You can install the lower version of azure.servicebus:

pip install azure-servicebus==7.0.0
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement