Azure / azure-iot-sdk-python

A Python SDK for connecting devices to Microsoft Azure IoT services
MIT License
425 stars 379 forks source link

[Technical Question] Cannot find azure.iot.hub.models #627

Closed leolauleolau closed 4 years ago

leolauleolau commented 4 years ago

I am trying to use the sample from microsoft(link below) to get device twin data

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-python-twin-getstarted#feedback

I have installed azure-iot-hub as shown in the instruction. I have tried pip3 install as well. Yet, still got the below error

"ModuleNotFoundError: No module named 'azure.iot.hub.models'"

Any ideas?

AB#7724107

zolvarga commented 4 years ago

@leolauleolau ,

Could you please provide the steps what you followed to get this error? I want to see how will it repro.

Zoltan

elhorton commented 4 years ago

hi @leolauleolau , is this still an issue for you? We will close this issue if you confirm it has been resolved. Thanks!

leolauleolau commented 4 years ago

@leolauleolau ,

Could you please provide the steps what you followed to get this error? I want to see how will it repro.

Zoltan

Sorry for the late reply. The issue is still here.

I am using a raspberry pi 4 2GB RAM to run the example script. I have a Python 3.7.3 IDLE.

After running the command pip install azure-iot-hub and pip3 install azure-iot-hub It gives out an error message as follow

Command "/usr/bin/python3 -u -c "import setuptools, tokenize;file='/tmp/pip-install-yc3l8gz0/uamqp/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-record-d_iugsya/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-yc3l8gz0/uamqp/

I ignore this and continue to run the code (which is the same as the document)

import sys
from time import sleep
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import Twin, TwinProperties, QuerySpecification, QueryResult
IOTHUB_CONNECTION_STRING = "[IoTHub Connection String]"

DEVICE_ID = "[Device Id]"
def iothub_service_sample_run():
    try:
        iothub_registry_manager = IoTHubRegistryManager(IOTHUB_CONNECTION_STRING)
        new_tags = {
                'location' : {
                    'region' : 'US',
                    'plant' : 'Redmond43'
                }
            }
        twin = iothub_registry_manager.get_twin(DEVICE_ID)
        twin_patch = Twin(tags=new_tags, properties= TwinProperties(desired={'power_level' : 1}))
        twin = iothub_registry_manager.update_twin(DEVICE_ID, twin_patch, twin.etag)
        # Add a delay to account for any latency before executing the query
        sleep(1)
        query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.location.plant = 'Redmond43'")
        query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100)
        print("Devices in Redmond43 plant: {}".format(', '.join([twin.device_id for twin in query_result.items])))
        print()
        query_spec = QuerySpecification(query="SELECT * FROM devices WHERE tags.location.plant = 'Redmond43' AND properties.reported.connectivity = 'cellular'")
        query_result = iothub_registry_manager.query_iot_hub(query_spec, None, 100)
        print("Devices in Redmond43 plant using cellular network: {}".format(', '.join([twin.device_id for twin in query_result.items])))
    except Exception as ex:
        print("Unexpected error {0}".format(ex))
        return
    except KeyboardInterrupt:
        print("IoT Hub Device Twin service sample stopped")
if __name__ == '__main__':
    print("Starting the Python IoT Hub Device Twin service sample...")
    print()
    iothub_service_sample_run()

And the error is it cannot find the module even I have restart the pi.

Traceback (most recent call last):

File "/home/pi/device twin.py", line 3, in from azure.iot.hub import IoTHubRegistryManager ModuleNotFoundError: No module named 'azure.iot.hub'

So, is it my command problem or the script? Thanks

zolvarga commented 4 years ago

@leolauleolau,

In the original issue the error was: "ModuleNotFoundError: No module named 'azure.iot.hub.models'"

In the second one it is: 'ModuleNotFoundError: No module named 'azure.iot.hub'

I cannot repro the problem and I think your Python installation is not working correctly. It looks like doesn't find modules in general. Is any other Python script working on your setup?

leolauleolau commented 4 years ago

@zolvarga

Both errors happened at different time. I have try the same script on vs code of my own computer. It works. Yet, this error came out when i executed on raspberry pi. The second error came out when I tried again one month later.

I have been sending messages to the cloud through pi using python with the module "azure.iot.device" and it works fine. This script was disabled before I ran the sample code.

elhorton commented 4 years ago

hi @leolauleolau , has this issue been resolved? it appears that it's outside the control of the Python SDK and rather related to the python runtime on the Pi-- let us know if you're still having issues. Otherwise we'll go ahead and close this issue. Thanks!

leolauleolau commented 4 years ago

Seems like pi's python problem. Thanks for the help

az-iot-builder-01 commented 4 years ago

@zolvarga, @leolauleolau, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey

ernijaime commented 1 year ago

Seems like pi's python problem. Thanks for the help

I am getting exactly the same issue, how did you solve it @leolauleolau ? I am using a Le Potato (Debian)

szetszho commented 1 year ago

Seems like pi's python problem. Thanks for the help

I am getting exactly the same issue, how did you solve it @leolauleolau ? I am using a Le Potato (Debian)

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
FAVetronics commented 1 year ago

I had the same: No /usr/local/lib/python3.9/dist-packages/azure/iot/hub folder Did a pip .... --force-reinstall - that helped