1

I have installed the pypi package azure-iot-device using pip as required to run a python file with the Azure IoT edge SDK. Doesn't work. I had even tried installing the azure package which I later realised was actually deprecated in 2020.

I still keep getting this ModuleNotFoundError, but it seems like all the demos I follow have done the same and aren't facing any errors. What's going on here?? I am using a conda environment where I have pip and installed the package.

Error:

Traceback (most recent call last):
  File "/Users/.../tempCodeRunnerFile.python", line 14, in <module>
    from azure.iot.device.aio import IoTHubDeviceClient
ModuleNotFoundError: No module named 'azure'

File running:

from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device import Message

# The device connection string to authenticate the device with your IoT hub.
CONNECTION_STRING = ****

MESSAGE_TIMEOUT = 10000

# Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
MSG_TXT = "{\"temperature\": %.2f,\"humidity\": %.2f}"

# Temperature threshold for alerting
TEMP_ALERT_THRESHOLD = 30


async def main():
    try:
        client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
        await client.connect()

        print("IoT Hub device sending periodic messages, press Ctrl-C to exit")

        while True:
            # Build the message with simulated telemetry values.
            temperature = TEMPERATURE + (random.random() * 15)
            humidity = HUMIDITY + (random.random() * 20)
            msg_txt_formatted = MSG_TXT % (temperature, humidity)
            message = Message(msg_txt_formatted)

            # Add standard message properties
            message.message_id = uuid.uuid4()
            message.content_encoding = "utf-8"
            message.content_type = "application/json"

            # Add a custom application property to the message.
            # An IoT hub can filter on these properties without access to the message body.
            prop_map = message.custom_properties
            prop_map["temperatureAlert"] = ("true" if temperature > TEMP_ALERT_THRESHOLD else "false")

            # Send the message.
            print("Sending message: %s" % message.data)
            try:
                await client.send_message(message)
            except Exception as ex:
                print("Error sending message from device: {}".format(ex))
            await asyncio.sleep(1)

    except Exception as iothub_error:
        print("Unexpected error %s from IoTHub" % iothub_error)
        return
    except asyncio.CancelledError:
        await client.shutdown()
        print('Shutting down device client')

if __name__ == '__main__':
    print("IoT Hub simulated device")
    print("Press Ctrl-C to exit")
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Keyboard Interrupt - sample stopped')

Thanks in advance!

Kiki
  • 11
  • 2

1 Answers1

0

Your post doesn't say how your environment is managed, where it is and such, but this is a problem I have often when using VSCode extension for Azure IOT tools.

It's a struggle to use conda to manage the azure IoT libraries. The VSCode and other tools don't work well in conda environments. Even though this is my go-to method for other python projects. Azure functions, IoT and Digital twins just work better if you use .venvs. My recommendation is to go in this direction when using IoT/edge tools.

This will create a .venv in your project directory, which can be found by VSCode tools:

python -m venv .venv
.venv\scripts\activate
pip install -r requirements.txt

Then when you use the VSCode tools. The upside is that your actual .venv is in your repo, not in your conda managemnt system (which can be somewhere else depending on how you are managing environments.)

Hope this helps.

billmanH
  • 1,298
  • 1
  • 14
  • 25