Search by Tags

Python Development and Debugging on TorizonCore Using Visual Studio Code

 

Article updated at 16 Sep 2020
Compare with Revision




Subscribe for this article updates

Select the version of your OS from the tabs below. If you don't know the version you are using, run the command cat /etc/os-release or cat /etc/issue on the board.

Torizon 5.0.0

Introduction

This article explains how to run and debug Python Applications on TorizonCore using the Visual Studio Code extension provided by Toradex.

This article complies to the Typographic Conventions for Torizon Documentation.

Prerequisites

Create a New Project

Press F1 on Visual Studio Code and then type Torizon/Python: Create Python Application to create an application:


  • Creating a Python Application

    Creating a Python Application

After creating it, write the application name on the same command bar:


  • Naming your Python Project

    Naming your Python Project

Now you should be able to select an application template, this will pre-create all the files required for a specific kind of application.

A bunch of options will appear, make sure to select the right target architecture:


  • Selecting target architecture

    Selecting target architecture

Note: For new projects we suggest choosing platforms based on debian bullseye, those are the platforms that will be maintained in the long term. Notice also that the list of platforms and templates may change after an update of the extension, providing new platforms for more recent versions of debian or templates for specific kinds of applications.

Type torizon on the username label:


  • Set Username

    Set Username

A python project with the following files will be created, to learn more about each one of them refer to the Python Project Configuration Files section.


  • Python project

    Python project


  • Pylint warning

    Pylint warning

Pylint warning is not specific for Torizon, it will happen for any .py file. Although not mandatory, a linter is really helpful and recommended during development.

Configure Project

Understand how to add Python packages and more in the next subsections.

Add Python Packages Available on Debian Feeds

Open the Torizon configuration page and edit the extrapackages configuration, you just need to click on the pen icon:


  • Extra packages

    Extra packages

As an example, add the package python3-pexpect:


  • Python3-pexpect package

    Python3-pexpect package

This would be the equivalent of running apt install python3-pexpect on the command-line. It does not use pip.

Python Project Configuration Files

The Torizon Extension for Visual Studio Code provides the following files for project configuration:

File Description Example Use Case
setup.sh Install additional components. Run apt-get install for Python libs that need native components.
requirements.txt Python packages to be installed via pip, one per line with optional version Please consider Add Python Packages Available on Debian Feeds as explained in the previous section.
This will simplify the management of binary components and ensure that the package version works with all the other libs provided in Debian.
Use pip only for packages that are pure Python code and are not available on Debian.
cleanup.sh runs after pip has installed packages in requirements.txt. Uninstall build-essential (for packages that build native code during setup).
.rsync-exclude-list list of the folders that will not be copied to the target and added to the application container. If you store files that are not meant to be shipped to the target in your app folder, add them to the list to save time and storage on deployment.

Deploy and Debug

First of all, move to the folder created on the previous steps and replace the main.py content with the following:

main.py
#!python
 
import pexpect
 
if __name__ == "__main__":
    child = pexpect.spawn("echo hello")
    print(child.readline())
 

After replacing the content, add a breakpoint on line 6 by clicking on the left side of the line, it will make a red dot appear.

Note: You may have to enable setting breakpoints anywhere in a file via File → Preferences → Settings → Debug: Allow Breakpoints Everywhere.

Let the debug process begin! Press F5 and the IDE will build and deploy the debug container image to the computer on module. The application runtime will stop on line 6:


  • Runtime breakpoint

    Runtime breakpoint

This process will take some time for the first time or whenever you edit requirement.txt, .sh files or the project settings.

Deploy and Release

After going on the process of deploying and debugging the application, you can now deploy and release it . Press F1 in Visual Studio Code to open the command bar and write the command Torizon/Python: Build release container for Python application.

Note: This entire process can take up to 1 minute.


  • Build release container command

    Build release container command

Your terminal output should be like the following:

Initializing Torizon Python application.
Downloading and running docker container used to enable emulation, this may take some time.
ARM emulation enabled.
Building release container (this may take some time)...
Release image has been built successfully.

Attention: Just note that the building process can take some time.

Now that your container was successfully built, you can press F1 again and enter the Torizon: Deploy release container, which deploys your container.

Running/restarting release container...
Container started.
Retrieving container information...
Application container is running.

You can confirm if the image was actually deployed to your Torizon device by running the following command on the module:

# docker images

To get more information about Docker Containers and their commands, please refer to Docker Oficial Documentation.

Torizon 4.0.0

Introduction

This article explains how to run and debug Python Applications on TorizonCore using the Visual Studio Code extension provided by Toradex.

This article complies to the Typographic Conventions for Torizon Documentation.

Prerequisites

Create a New Project

Press F1 on Visual Studio Code and then type Torizon/Python: Create Python Application to create an application:


  • Creating a Python Application

    Creating a Python Application

After creating it, write the application name on the same command bar:


  • Naming your Python Project

    Naming your Python Project

A bunch of options will appear, make sure to select the right target architecture:


  • Selecting target architecture

    Selecting target architecture

Type torizon on the username label:


  • Set Username

    Set Username

A python project with the following files will be created, to learn more about each one of them refer to the Python Project Configuration Files section.


  • Python project

    Python project


  • Pylint warning

    Pylint warning

Pylint warning is not specific for Torizon, it will happen for any .py file. Although not mandatory, a linter is really helpful and recommended during development.

Configure Project

Understand how to add Python packages and more in the next subsections.

Add Python Packages Available on Debian Feeds

Open the Torizon configuration page and edit the extrapackages configuration, you just need to click on the pen icon:


  • Extra packages

    Extra packages

As an example, add the package python3-pexpect:


  • Python3-pexpect package

    Python3-pexpect package

This would be the equivalent of running apt install python3-pexpect on the command-line. It does not use pip.

Python Project Configuration Files

The Torizon Extension for Visual Studio Code provides the following files for project configuration:

File Description Example Use Case
setup.sh Install additional components. Run apt-get install for Python libs that need native components.
requirements.txt Python packages to be installed via pip, one per line with optional version Please consider Add Python Packages Available on Debian Feeds as explained in the previous section.
This will simplify the management of binary components and ensure that the package version works with all the other libs provided in Debian.
Use pip only for packages that are pure Python code and are not available on Debian.
cleanup.sh runs after pip has installed packages in requirements.txt. Uninstall build-essential (for packages that build native code during setup).
.rsync-exclude-list list of the folders that will not be copied to the target and added to the application container. If you store files that are not meant to be shipped to the target in your app folder, add them to the list to save time and storage on deployment.

Deploy and Debug

First of all, move to the folder created on the previous steps and replace the main.py content with the following:

main.py
#!python
 
import pexpect
 
if __name__ == "__main__":
    child = pexpect.spawn("echo hello")
    print(child.readline())
 

After replacing the content, add a breakpoint on line 6 by clicking on the left side of the line, it will make a red dot appear.

Note: You may have to enable setting breakpoints anywhere in a file via File → Preferences → Settings → Debug: Allow Breakpoints Everywhere.

Let the debug process begin! Press F5 and the IDE will build and deploy the debug container image to the computer on module. The application runtime will stop on line 6:


  • Runtime breakpoint

    Runtime breakpoint

This process will take some time for the first time or whenever you edit requirement.txt, .sh files or the project settings.

Deploy and Release

After going on the process of deploying and debugging the application, you can now deploy and release it . Press F1 in Visual Studio Code to open the command bar and write the command Torizon/Python: Build release container for Python application.

Note: This entire process can take up to 1 minute.


  • Build release container command

    Build release container command

Your terminal output should be like the following:

Initializing Torizon Python application.
Downloading and running docker container used to enable emulation, this may take some time.
ARM emulation enabled.
Building release container (this may take some time)...
Release image has been built successfully.

Attention: Just note that the building process can take some time.

Now that your container was successfully built, you can press F1 again and enter the Torizon: Deploy release container, which deploys your container.

Running/restarting release container...
Container started.
Retrieving container information...
Application container is running.

You can confirm if the image was actually deployed to your Torizon device by running the following command on the module:

# docker images

To get more information about Docker Containers and their commands, please refer to Docker Oficial Documentation.