简体   繁体   中英

Error using azure-eventhubs python example

I'm attempting to use the example code to receive data from Azure Event Hub.

Heres the code

import os
import sys
import logging
import time
from azure.eventhub import EventHubClient, Receiver, Offset

logger = logging.getLogger("azure")

ADDRESS = "amqps://mine.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sadfsafdljksad=<eventhub>"

But I am getting the following error when executing..

Traceback (most recent call last):
  File "./recv.py", line 5, in <module>
    from azure.eventhub import EventHubClient, Receiver, Offset
ImportError: cannot import name 'EventHubClient'

Any assistance will be appreciated.

I think you actually installed azure-eventhub package, version 5.0.0 . But EventHubClient does not exist in 5.0.0 version, it exists in azure-eventhub package, version 1.3.3 or below.

By default, when you install the azure eventhub package for python using this command pip install azure-eventhub , it will automatically install the latest version 5.0.0. So this sentence from azure.eventhub import EventHubClient, Receiver, Offset will throw the error ImportError: cannot import name 'EventHubClient'

You can check the version of the package by open cmd -> input command pip show azure-eventhub . Screenshot as below:

在此处输入图片说明

There are 2 solutions:

Solution 1: keep using the 5.0.0 version, then you should re-write your code and follow the examples in this doc .

Solution 2: If you want to use the current code which includes from azure.eventhub import EventHubClient, Receiver, Offset , you should first uninstall the 5.0.0 version of azure-eventhub, then reinstall the 1.3.3 version by using this command pip install azure-eventhub==1.3.3

使用此解决方案可以正常使用此命令重新安装 1.3.3 版本 pip install azure-eventhub==1.3.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM