Search by Tags

Python Development and Debugging on TorizonCore Using Visual Studio Code

 

Article updated at 01 Dec 2020
Compare with Revision




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.



Remember that you can always refer to the Torizon Documentation, there you can find a lot of relevant articles that might help you in the application development.

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

You will be able to select a template. Some examples are a console or a Qt QML 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.

Set the Default Python Interpreter

Some operating systems still point the python executable to Python 2, others may only use the python3 executable. You must make sure that Visual Studio Code is using a version of Python 3 as the default interpreter, otherwise, you may see an error message Path to shell executable "python" does not exist.

Press F1 and run the command Python: Select Interpreter:


  • Run "Python: Select Interpreter"

    Run "Python: Select Interpreter"

Select any version of Python 3:


  • Select a Version of Python 3

    Select a Version of Python 3

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

After configuring your project, you can now deploy and debug it. Since this is common to all languages supported by the extension, please refer to the section Deploy and Debug from the article Visual Studio Code Extension for Torizon.

Deploy and Release

After going through the process of deploying and debugging the application, you can now deploy and release it. Since this is common to all languages supported by the extension, please refer to the section Deploy and Release from the article Visual Studio Code Extension for Torizon.

Torizon 4.0.0

Update to a Newer Debian Container Version

Debian Containers for Torizon are subject to updates, including major updates to newer versions of Debian. Learn more about it, including how to update your VS Code project:

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.