2

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)
Shadi
  • 193
  • 2
  • 12
  • This code seems to have no problem on my side, maybe you can try to reinstall `azure.servicebus`. Or you can try the [solution](https://github.com/MicrosoftDocs/azure-docs/issues/25699) mentioned here. – Frank Borzage Apr 23 '21 at 03:02
  • @FrankGong I install `azure.servicebus`. Also, I tried link solution and add `control_client` but I had the same problem. – Shadi Apr 23 '21 at 07:04
  • Maybe you can try to install a lower version of `azure.servicebus`: `pip install azure-servicebus==7.0.0` – Frank Borzage Apr 23 '21 at 09:43
  • @FrankGong Yes that's right. This problem solved with `pip install azure-servicebus==7.0.0` – Shadi Apr 23 '21 at 09:59

2 Answers2

2

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

pip install azure-servicebus==7.0.0
Frank Borzage
  • 6,292
  • 1
  • 6
  • 19
  • Incompatible versions _of what_? It's just one thing being installed. The 7.0.0 version does not work for me. – Harvastum Aug 29 '22 at 00:26
1

In my case had to go down to further lower version

pip install azure-servicebus==0.21.1

You could check release versions here: https://pypi.org/project/azure-servicebus/#history

Aman Gupta
  • 3,627
  • 4
  • 40
  • 64