Skip to content

ImportError: cannot import name 'Client' from partially initialized module 'alibabacloud_iot20180120.client' #38

Description

@WangGuangxu2827

这是官方提供的实例代码:
`# -- coding: utf-8 --

This file is auto-generated, don't edit it. Thanks.

import os
import sys

from typing import List

from alibabacloud_iot20180120.client import Client as Iot20180120Client
from alibabacloud_credentials.client import Client as CredentialClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_iot20180120 import models as iot_20180120_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient

class Sample:
def init(self):
pass

@staticmethod
def create_client() -> Iot20180120Client:
    """
    使用凭据初始化账号Client
    @return: Client
    @throws Exception
    """
    # 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378659.html。
    credential = CredentialClient()
    config = open_api_models.Config(
        credential=credential
    )
    # Endpoint 请参考 https://api.aliyun.com/product/Iot
    config.endpoint = f'iot.cn-shanghai.aliyuncs.com'
    return Iot20180120Client(config)

@staticmethod
def main(
    args: List[str],
) -> None:
    client = Sample.create_client()
    pub_request = iot_20180120_models.PubRequest(
        device_name='',
        iot_instance_id='iot-06z00axxxx',
        product_key='xxxxxxx',
        topic_full_name='/sys/xxxxxxx/xxxx/thing/service/property/set',
        message_content='This is api test'
    )
    runtime = util_models.RuntimeOptions()
    try:
        # 复制代码运行请自行打印 API 的返回值
        client.pub_with_options(pub_request, runtime)
    except Exception as error:
        # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        # 错误 message
        print(error.message)
        # 诊断地址
        print(error.data.get("Recommend"))
        UtilClient.assert_as_string(error.message)

@staticmethod
async def main_async(
    args: List[str],
) -> None:
    client = Sample.create_client()
    pub_request = iot_20180120_models.PubRequest(
        device_name='',
        iot_instance_id='iot-xxxx',
        product_key='xxxxx',
        topic_full_name='/sys/xxxx/xxxxx/thing/service/property/set',
        message_content='This is api test'
    )
    runtime = util_models.RuntimeOptions()
    try:
        # 复制代码运行请自行打印 API 的返回值
        await client.pub_with_options_async(pub_request, runtime)
    except Exception as error:
        # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        # 错误 message
        print(error.message)
        # 诊断地址
        print(error.data.get("Recommend"))
        UtilClient.assert_as_string(error.message)

if name == 'main':
Sample.main(sys.argv[1:])
`

Image

但是我自己下载运行时发生报错:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions