Linux no module named azure.iot.hub

262 Views Asked by At

I'm having an issue trying to execute a very simple python script that works perfectly in Windows but not on my Le Potato (Debian):

import uuid
from azure.iot.hub import IoTHubRegistryManager
import MqttClient as mqttClient


CONNECTION_STRING = "HostName=(...)"
DEVICE_ID = "(...)"
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)

def send_message(msg):
    props = {}
    props.update(messageId = str(uuid.uuid4()))
    props.update(contentType = "application/json")
    props.update()
    registry_manager.send_c2d_message(DEVICE_ID, msg, properties=props)


def on_message(mqttc, obj, msg):
    print("[" + msg.topic+"]: "+str(msg.payload))
    try:
        send_message(str(msg.payload))
    except Exception as e:
        print("An exception occurred:", str(e))

mqttClient.init(on_message=on_message)

When running python myscript.py I get the error ModuleNotFoundError: No module named 'azure.iot.hub'

I installed everything several times:

  • pip install azure-iot-hub

  • pip install azure-iot-device

And I even created my own virtual environment with a fresh install and tried the same exact versions in Windows.

Update: other tests

I have tried some more things:

  1. pip show azure-iot-hub prints the following:
Name: azure-iot-hub
(...)
Location: /usr/local/lib/python3.9/dist-packages
Requires: azure-core, msrest, uamqp
Required-by:

It seems it installs the packages on /usr/local and my python installation seems to be somewhere else:

>>> import sys
>>> print(sys.executable)
/usr/bin/python
>>> print(sys.path)
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/offiman/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages']

So I tried to move every package from /usr/local/lib/python3.9/dist-packages/ to /usr/lib/python3.9/dist-packages/. Still nothing.

  1. Folder /usr/local/lib/python3.9/dist-packages/azure/iot is completely empty, seems weird.
3

There are 3 best solutions below

0
Max Sze On BEST ANSWER

Same issues with you on RPi4 running fresh installed Bullseye. Was not able to import azure.iot.hub modules both from python script and cmdline interrupter. While all other azure modules i.e. azure.core / azure.iot.device work find and was installed the same way as azure.iot.hub in same dir.

I tried installing the module via all different method, with/wihtout sudo, pip, pip3, python -m pip , python3 -m pip... all to make sure the its installed properly.

In my case, i end up finding the module was not actually installed in the path. And have to manually copy the source file over to fix the issues.(/usr/local/lib/python3.9/dist-packages/azure/iot/hub is missing)

  1. Install module via pip python3 -m pip install azure_iot_hub
  2. Check the installation path for the module pip3 list -v, in my case, i have the module located at /usr/local/lib/python3.9/dist-packages/
  3. Download the Built Distributions from pypl : https://pypi.org/project/azure-iot-hub/#files
  4. Copy the source file form ../azure/iot/hub in the zip file, to the installation path, i.e./usr/local/lib/python3.9/dist-packages/azure/iot/hub
  5. That should fix the problem
1
Sampath On

Using this reference MSDOC able to Send cloud-to-device messages.

enter image description here

  • Install pip install azure-iot-hub , pip3 install azure-iot-hub andpip3 install uuid
  • With pip list we can check the respective module is installed .

enter image description here

enter image description here

Code:

import  random
import  uuid
from  azure.iot.hub  import  IoTHubRegistryManager
CONNECTION_STRING = "HostName=sampath23sa454.azure-devices.net;SharedAccessKeyName=iot"
DEVICE_ID = "sampath23"
registry_manager = IoTHubRegistryManager(CONNECTION_STRING)
def  send_message(msg):
props = {
"messageId":  str(uuid.uuid4()),
"contentType":  "application/json"
}
registry_manager.send_c2d_message(DEVICE_ID,  msg,  properties=props)
print("Message sent successfully!")
print("Message content:",  msg)
def  on_message(mqttc,  obj,  msg):
print("[" + msg.topic + "]: " + str(msg.payload))
try:
send_message(str(msg.payload))
except  Exception  as  e:
print("An exception occurred:",  str(e))
class  MqttClient:
@staticmethod
def  init(on_message):
pass
mqttClient = MqttClient()
mqttClient.init(on_message=on_message)
if  __name__ == '__main__':
print("Starting the IoT Hub C2D Messaging sample...")
message_to_send = "Hello from the cloud!"
send_message(message_to_send)
input("Press Enter to stop...\n")
print("IoT Hub C2D Messaging sample stopped")

Output:

enter image description here

Code:

mqttClient = MqttClient()
mqttClient.init(on_message=on_message)
if  __name__ == '__main__':
print("Starting the IoT Hub C2D Messaging sample...")
random_messages = ["Temperature: 25°C",  "Humidity: 50%",  "Pressure: 1013 hPa"]
for  _  in  range(5):  # Sending 5 random messages
random_message = random.choice(random_messages)
send_message(random_message)
input("Press Enter to stop...\n")
print("IoT Hub C2D Messaging sample stopped")

Output:

enter image description here

7
hamed danesh On

linux see root and non-root as different users, are you running your python code at the same user you installed these dependencies? for example if you run it by sudo, you need to reinstall those libraries on root too.