python - 为什么我不断收到模块 'azure' 未找到错误? [Azure IoT Edge 特定]

标签 python azure azure-iot-hub azure-iot-edge azure-iot-sdk

我已根据需要使用 pip 安装了 pypi 包 azure-iot-device,以便通过 Azure IoT Edge SDK 运行 python 文件。不起作用。 我什至尝试安装 azure 软件包,后来我意识到该软件包实际上已在 2020 年被弃用。

我仍然不断收到此 ModuleNotFoundError,但似乎我关注的所有演示都做了同样的事情并且没有遇到任何错误。这里发生了什么?? 我正在使用 conda 环境,其中有 pip 并安装了软件包。

错误:

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'

文件运行:

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')

提前致谢!

最佳答案

您的帖子没有说明您的环境是如何管理的、在哪里等,但这是我在使用 Azure IOT 工具的 VSCode 扩展时经常遇到的问题。

使用 conda 来管理 azure IoT 库是一件很困难的事情。 VSCode 和其他工具在 conda 环境中无法正常工作。尽管这是我其他 python 项目的首选方法。如果使用 .venv,Azure 函数、物联网和数字孪生会工作得更好。我的建议是在使用物联网/边缘工具时朝这个方向发展。

这将在您的项目目录中创建一个 .venv,可以通过 VSCode 工具找到它:

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

然后当你使用VSCode工具时。好处是您的实际 .venv 位于您的存储库中,而不是您的 conda 管理系统中(根据您管理环境的方式,它可能位于其他位置。)

希望这有帮助。

关于python - 为什么我不断收到模块 'azure' 未找到错误? [Azure IoT Edge 特定],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74007239/

相关文章:

php - Azure Linux Web 应用程序直接从 URL 下载文件不起作用

powershell - 获取控制台模式时发生 Win32 内部错误 "The handle is invalid"0x6

Azure IoT 将文件上传到 Blob

azure - Azure Application Insights 与 Google 搜索之间的 IP 地址不匹配

azure - 如何测试到达 Azure IoTHub 的消息

azure - 通过 azure-cli 监控 azure-iot 部署

python - Pandas 仅在条件为真时才替换数据框中列的值

python - 在 Python 中指定小数位数

python - 使用 setuptools 可执行访问 yaml 配置文件

python - 如何根据 Django 中的两个字段对查询集进行排序?