Getting started with Python#

Before you start#

Before we can use the SimpleSwitch™ SDK for Python we need to download the SDK extension. For this you need to run from the sourced SimpleSwitch™ SDK

$ GITHUB_TOKEN="<your personal access token for Github>" GITHUB_USERNAME="<your Github username>" simpleswitch-sdk-tool install python
$ eval "$(simpleswitch-sdk-tool activate)"

Github personal access token

To create a Github personal access token see Github container registry (GHCR)

Start a Python project#

Example Python Project#

Here is a very small Python project, with a single Python source file, src/app.py:

#!/usr/bin/env python3
# SPDX-FileCopyrightText: (C) 2022 Avnet Embedded GmbH
# SPDX-License-Identifier: LicenseRef-Avnet-OSS-1.0

"""Python test."""

import logging
import time

if __name__ == "__main__":
    while True:
        logging.warning("Python test ok")
        time.sleep(5)

Build the Python project#

Using simpleswitch-generate-package with Python#

You can also create a package by copying the content of a directory. All you need is this directory to reproduce the structure desired in the package

$ find deploy_dir -printf "%y %p\n"
d deploy_dir
d deploy_dir/usr
d deploy_dir/usr/bin
f deploy_dir/usr/bin/app.py

To do it just create the desired directory

$ mkdir -p deploy_dir/usr/bin

Then copy the Python script and give it execution rights

$ cp src/app.py deploy_dir/usr/bin
$ chmod +x deploy_dir/usr/bin/app.py

You can then use the --copy-dir option of the simpleswitch-generate-package the Generate a SimpleSwitch™ package script

$ simpleswitch-generate-package --name simpleswitch-example --copy-dir deploy_dir \
 --template python --startup-command /usr/bin/app.py

Or if you want to create a debuggable version run

$ simpleswitch-generate-package --name simpleswitch-example --copy-dir deploy_dir \
    --template python --startup-command "/usr/bin/python3 -m debugpy --listen 0.0.0.0:5678 --wait-for-client /usr/bin/app.py"

Deploy to the target#

Now it is time to deploy the generated SimpleSwitch™ container to the device. For this please see Deploy a SimpleSwitch™ package

Remote debugging a Python application#

For debugging python we are using the Debug Adapter Protocol which is also supported by several other tools.

Further reading#