%3CLINGO-SUB%20id%3D%22lingo-sub-1201894%22%20slang%3D%22en-US%22%3ENew%20version%20of%20the%20Python%20SDK%20released%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1201894%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20recently%20released%20a%20new%20version%20of%20the%20Python%20SDK.%20We%E2%80%99re%20working%20hard%20to%20improve%20the%20experience%20of%20our%20developers%20and%20we%20heard%20your%20feedback%E2%80%94the%20Python%20SDK%20is%20no%20longer%20a%20wrapper%20around%20C%20code%2C%20it%E2%80%99s%20native%20Python!%20For%20those%20of%20you%20who%20haven%E2%80%99t%20seen%20this%20yet%2C%20here%E2%80%99s%20a%20quick%20overview%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhat%20is%20the%20v2%20Python%20SDK%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EIt%E2%80%99s%20a%20newly%20designed%2C%20%3CEM%3EPython-native%3C%2FEM%3E%20SDK%20for%20working%20with%20Azure%20IoT.%20If%20you%E2%80%99re%20already%20familiar%20with%20our%20Python%20SDK%2C%20then%20you%20know%20that%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Ftree%2Fv1-deprecated%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eour%20first%20version%20(v1)%3C%2FA%3E%20was%20a%20wrapper%20around%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-c%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eour%20C%20SDK%3C%2FA%3E.%20Like%20anything%2C%20this%20approach%20had%20its%20pros%20and%20cons.%20On%20the%20upside%2C%20developers%20familiar%20with%20the%20C%20SDK%20could%20further%20expand%20to%20Python%20with%20no%20underlying%20functionality%20differences.%20However%2C%20since%20the%20SDK%20was%20not%20in%20native%20Python%2C%20the%20developmental%20ease%20typically%20associated%20with%20Python%20was%20certainly%20lacking.%20With%20this%20new%20design%2C%20the%20v2%20SDK%20aims%20to%20provide%20a%20simplified%2C%20more%20natural%20experience%20for%20Python%20developers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhy%20should%20you%20upgrade%20to%20v2%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ESimply%20put%2C%20because%20the%20developer%20experience%20is%20so%20much%20better!%3C%2FP%3E%0A%3CP%3EWhereas%20the%20installation%20process%20for%20v1%20involved%20a%20series%20of%20steps%20and%20was%20highly%20dependent%20on%20your%20development%20configuration%2C%20setting%20up%20v2%20is%20as%20easy%20as%20running%20a%20pip%20command.%20Additionally%2C%20the%20APIs%20look%20and%20feel%20what%20they%20are%E2%80%94Python%20native.%3C%2FP%3E%0A%3CP%3ETo%20make%20it%20easier%20to%20transition%20your%20solution%20code%20from%20v1%20to%20v2%2C%20check%20out%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Fblob%2Fmaster%2Fmigration_guide.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20migration%20guide%3C%2FA%3E%20for%20common%20scenarios%2C%20along%20with%20our%20more%20detailed%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Ftree%2Fmaster%2Fazure-iot-device%2Fsamples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecollection%20of%20samples%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20briefly%20illustrate%20the%20new%20look%20and%20feel%20of%20the%20SDK%2C%20here's%20a%20couple%20examples%20of%20the%20implementation%20differences%20between%20v1%20and%20v2%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20for%20sending%20telemetry%20to%20your%20IoT%20Hub%2C%20you'd%20write%20the%20following%20in%26nbsp%3B%3CSTRONG%3Ev1%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%20%20%20%20message%20%3D%20IoTHubMessage(%22telemetry%20message%22)%0A%20%20%20%20prop_map%20%3D%20message.properties()%0A%20%20%20%20prop_map.add(%22property%22%2C%20%22property_value%22)%0A%20%20%20%20client.send_event_async(message%2C%20send_confirmation_callback%2C%20user_ctx)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhereas%20in%20%3CSTRONG%3Ev2%3C%2FSTRONG%3E%2C%20you%20can%20simplify%20with%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%20%20%20%20message%20%3D%20Message(%22telemetry%20message%22)%0A%20%20%20%20message.custom_properties%5B%22property%22%5D%20%3D%20%22property_value%22%0A%20%20%20%20client.send_message(message)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20an%20even%20more%20pronounced%20example%2C%20in%20%3CSTRONG%3Ev1%20%3C%2FSTRONG%3Eyou%E2%80%99d%20write%20the%20following%20for%20receiving%20a%20message%20from%20your%20IoT%20Hub%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Edef%20receive_message_callback(message%2C%20counter)%3A%0A%20%20%20%20%20%20%20%20global%20RECEIVE_CALLBACKS%0A%20%20%20%20%20%20%20%20message%20%3D%20message.get_bytearray()%0A%20%20%20%20%20%20%20%20size%20%3D%20len(message_buffer)%0A%20%20%20%20%20%20%20%20print%20(%20%22the%20data%20in%20the%20message%20received%20was%20%3A%20%26lt%3B%26lt%3B%26lt%3B%25s%26gt%3B%26gt%3B%26gt%3B%20%26amp%3B%20Size%3D%25d%22%20%25%20(message_buffer%5B%3Asize%5D.decode('utf-8')%2C%20size)%20)%0A%20%20%20%20%20%20%20%20map_properties%20%3D%20message.properties()%0A%20%20%20%20%20%20%20%20key_value_pair%20%3D%20map_properties.get_internals()%0A%20%20%20%20%20%20%20%20print%20(%20%22custom%20properties%20are%3A%20%25s%22%20%25%20key_value_pair%20)%0A%20%20%20%20%20%20%20%20return%20IoTHubMessageDispositionResult.ACCEPTED%0A%0A%20%20%20%20client.set_message_callback(message_listener_callback%2C%20RECEIVE_CONTEXT)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20in%20the%20newly%20designed%20%3CSTRONG%3Ev2%20%3C%2FSTRONG%3ESDK%2C%20you%20write%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3Edef%20message_listener(client)%3A%0A%20%20%20%20%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20message%20%3D%20client.receive_message()%20%20%23%20blocking%20call%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22the%20data%20in%20the%20message%20received%20was%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(message.data)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22custom%20properties%20are%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(message.custom_properties)%0A%0A%20%20%20%20%23%20Run%20a%20listener%20thread%20in%20the%20background%0A%20%20%20%20listen_thread%20%3D%20threading.Thread(target%3Dmessage_listener%2C%20args%3D(device_client%2C))%0A%20%20%20%20listen_thread.daemon%20%3D%20True%0A%20%20%20%20listen_thread.start()%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EExtra%20perks%20with%20V2!%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EWhile%20v2%20provides%20all%20the%20same%20support%20you%E2%80%99ve%20come%20to%20expect%20for%20getting%20your%20IoT%20devices%20up%20and%20running%2C%20it%20also%20has%20a%20few%20changes%3A%3C%2FP%3E%0A%3CP%3EThe%20Python%20v2%20SDK%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Enow%20has%20support%20for%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Fblob%2Fmaster%2Fazure-iot-device%2FREADME.md%23iot-hub-module-client%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emodule%20clients%3C%2FA%3E!%20Just%20as%20you%20can%20use%20our%20SDK%20to%20connect%20devices%2C%20you%20can%20now%20use%20Python%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Ftree%2Fa724fadf1881290028b7a4ab8a77ae7a36909531%2Fazure-iot-device%2Fsamples%2Fasync-edge-scenarios%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eto%20connect%20Azure%20IoT%20Edge%20Hubs%20and%20Azure%20IoT%20Hubs%3C%2FA%3E%20as%20well.%3C%2FLI%3E%0A%3CLI%3Esupports%20an%20asynchronous%20API%2C%20along%20with%20a%20blocking%20synchronous%20API%20for%20compatibility%20scenarios.%3C%2FLI%3E%0A%3CLI%3Eallows%20you%20to%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Fblob%2F5e4c4e70bbcc88f5f0b6a5f950ae445a6c376bec%2Fazure-iot-device%2Fsamples%2Fasync-hub-scenarios%2Fupload_to_blob.py%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edirectly%20upload%20to%20Azure%20blob%20storage%3C%2FA%3E!%3C%2FLI%3E%0A%3CLI%3Eonly%20supports%20the%20MQTT%20protocol%20to%20simplify%20the%20connection%20experience.%20To%20see%20how%20to%20use%20web%20sockets%2C%20check%20out%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Fblob%2Fmaster%2Fazure-iot-device%2Fsamples%2Fasync-hub-scenarios%2Fsend_message_via_proxy.py%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20sample%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%E2%80%99ve%20also%20recently%20released%20a%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2Freleases%2Ftag%2F2020-02-27%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enew%20version%20(2.0.1)%3C%2FA%3E%3C%2FSTRONG%3E%20that%20includes%20support%20for%20cloud-to-device%20messaging%20and%20improves%20MQTT%20connection%20resiliency.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%E2%80%99re%20excited%20to%20see%20what%20you%20build%20with%20our%20new%20and%20improved%20Python%20SDK!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20we%20continue%20to%20work%20on%20improving%20the%20experience%20for%20Python%20developers%2C%20we%20encourage%20you%20to%20provide%20feedback%20through%20our%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-iot-sdk-python%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eopen%20source%20Github%20repository%3C%2FA%3E%20and%20stay%20tuned%20on%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fupdates%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20updates%3C%2FA%3E%20for%20any%20new%20IoT%20SDK%20announcements.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1201894%22%20slang%3D%22en-US%22%3E%3CP%3ELearn%20about%20what%20the%20v2%20Python%20SDK%20for%20Azure%20IoT%20has%20to%20offer!%20We%E2%80%99re%20working%20hard%20to%20improve%20the%20experience%20of%20our%20developers%20and%20we%20heard%20your%20feedback%E2%80%94the%20Python%20SDK%20is%20no%20longer%20a%20wrapper%20around%20C%20code%2C%20it%E2%80%99s%20native%20Python!%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

We recently released a new version of the Python SDK. We’re working hard to improve the experience of our developers and we heard your feedback—the Python SDK is no longer a wrapper around C code, it’s native Python! For those of you who haven’t seen this yet, here’s a quick overview:

 

What is the v2 Python SDK?

It’s a newly designed, Python-native SDK for working with Azure IoT. If you’re already familiar with our Python SDK, then you know that our first version (v1) was a wrapper around our C SDK. Like anything, this approach had its pros and cons. On the upside, developers familiar with the C SDK could further expand to Python with no underlying functionality differences. However, since the SDK was not in native Python, the developmental ease typically associated with Python was certainly lacking. With this new design, the v2 SDK aims to provide a simplified, more natural experience for Python developers.

 

Why should you upgrade to v2?

Simply put, because the developer experience is so much better!

Whereas the installation process for v1 involved a series of steps and was highly dependent on your development configuration, setting up v2 is as easy as running a pip command. Additionally, the APIs look and feel what they are—Python native.

To make it easier to transition your solution code from v1 to v2, check out this migration guide for common scenarios, along with our more detailed collection of samples.

 

To briefly illustrate the new look and feel of the SDK, here's a couple examples of the implementation differences between v1 and v2: 

First, for sending telemetry to your IoT Hub, you'd write the following in v1:

 

 

    message = IoTHubMessage("telemetry message")
    prop_map = message.properties()
    prop_map.add("property", "property_value")
    client.send_event_async(message, send_confirmation_callback, user_ctx)

 

 

Whereas in v2, you can simplify with:

 

 

    message = Message("telemetry message")
    message.custom_properties["property"] = "property_value"
    client.send_message(message)

 

 

As an even more pronounced example, in v1 you’d write the following for receiving a message from your IoT Hub:

 

 

def receive_message_callback(message, counter):
        global RECEIVE_CALLBACKS
        message = message.get_bytearray()
        size = len(message_buffer)
        print ( "the data in the message received was : <<<%s>>> & Size=%d" % (message_buffer[:size].decode('utf-8'), size) )
        map_properties = message.properties()
        key_value_pair = map_properties.get_internals()
        print ( "custom properties are: %s" % key_value_pair )
        return IoTHubMessageDispositionResult.ACCEPTED

    client.set_message_callback(message_listener_callback, RECEIVE_CONTEXT)

 

 

But in the newly designed v2 SDK, you write:

 

 

def message_listener(client):
        while True:
            message = client.receive_message()  # blocking call
            print("the data in the message received was ")
            print(message.data)
            print("custom properties are")
            print(message.custom_properties)

    # Run a listener thread in the background
    listen_thread = threading.Thread(target=message_listener, args=(device_client,))
    listen_thread.daemon = True
    listen_thread.start()

 

 

 

Extra perks with V2!

While v2 provides all the same support you’ve come to expect for getting your IoT devices up and running, it also has a few changes:

The Python v2 SDK:

We’ve also recently released a new version (2.0.1) that includes support for cloud-to-device messaging and improves MQTT connection resiliency.

 

We’re excited to see what you build with our new and improved Python SDK!

 

As we continue to work on improving the experience for Python developers, we encourage you to provide feedback through our open source Github repository and stay tuned on Azure updates for any new IoT SDK announcements.