Search by Tags

Build and Run Your First Python Application

 

Your progress

 

Overview

When it comes to Linux development, it is possible to choose several IDEs, workflows and programming languages for development. Aiming to simplify your development experience Toradex provides TorizonCore IDE integration with Visual Studio Code for Windows and Linux PCs, and Visual Studio for Windows PCs.

In this module you will:

  • Use the Visual Studio Code IDE.
  • Develop your application using the Torizon Extension for Visual Studio Code: Toradex Torizon Support.
  • Deploy it to the target module and debug it remotely from your development PC.

Note: support for a more complex workflow on Visual Studio Code will be presented to you at the end of the quickstart guide, as well as support for other programming languages on Visual Studio. If you have a Windows PC for development, you can also try the quickstart guide for Torizon with Windows selected as Development PC OS.

Typographic Conventions

Prerequisites

For this Quickstart Guide:

  • Development computer with Ubuntu 18.04 LTS. Warning: Commands and instructions may vary slightly on other Ubuntu releases and Linux distributions.
  • Successfully completed the previous lessons from this guide.

For this lesson:

Note: Carefully read this module's cover page clicking on "Module 2: First Steps with Torizon" on the left menu bar before starting this lesson.

Step 1

The latest versions of the Toradex Torizon Support extension can be found on the Visual Studio Code marketplace, in two versions:

Click on the Extensions button in Visual Studio Code as shown in the image below and search for Toradex Torizon Support, then select the option you prefer among the ones presented above:


  • Toradex Torizon Support Extension for Visual Studio Code

    Toradex Torizon Support Extension for Visual Studio Code

Verify that the extension has been enabled:


  • Torizon extension

    Torizon extension

Step 2

Click the Torizon logo on the left vertical menu, then click on Add Device:


  • Add Device

    Add Device

Step 3

Add a Network device:


  • Choose to Add From Network

    Choose to Add From Network

Use the previously found hostname or IP address:


  • Add Device Hostname or IP

    Add Device Hostname or IP

Leave the username as torizon:


  • Set Username

    Set Username

Add the password you have previously set during the first login to the board:


  • Add Password

    Add Password

Step 4

Wait one or two minutes, as the board reboots.

Step 5

Click on the Refresh button, on the Devices list where your device will be listed if successfully added:


  • Refresh Your Device Resource List

    Refresh Your Device Resource List

You can see which container images are deployed to the device and which containers are instanced - either running or stopped - among other properties:


  • Browse Device Resources

    Browse Device Resources

Step 6

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


  • Create Python Application

    Create Python Application

Add an Application name. We will name it quickstart as an example:


  • Add Application Name

    Add Application Name

Select your platform. Ignore the Qt options for now:


  • Select Platform

    Select Platform

Choose the version of the container and the architecture:


  • Choose Architecture

    Choose Architecture

Make sure that the Username is torizon when prompted:


  • Set Username

    Set Username

Your project template will be displayed in the file explorer:


  • Project Template

    Project Template


  • 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.

Step 7

Open the main.py file and slightly modify it to make our debug fun:

main.py
#!python
 
if __name__ == "__main__":
    counter = 1
    for i in range(5):
        counter = 2*counter
        print("Loop " + str(i) + " - var=" + str(counter))

Step 8

Add a breakpoint on line 7. You just have to click on the beginning of the line:

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


  • Add a Breakpoint

    Add a Breakpoint

Step 9

Press F5 to start the process of deploying your application and debugging. It takes a few minutes when executed for the first time. Shall we grab a coffee?

When the process finishes, the Visual Studio Code will automatically switch to the Debug option on the vertical menu on the left and your code will stop on line 7 where you have inserted the breakpoint:


  • Debug Environment

    Debug Environment

Step 10

Press F5 or click the play button to run until the next breakpoint and so on. You can see that the values of variables change as you go, and the output of code is printed to the Debug Console:

  • Debugging the Sample Application

It is interesting to highlight that you are doing remote debugging, which means that the application is running on the board, not on your development PC!

Takeaway

As the Quickstart is a step-by-step guide, the current lesson is only a brief introduction to application development with Torizon. For a great development experience, you can choose to use the Visual Studio Extension For Torizon and write your application in C/C++ or use the Visual Studio Code Extension for Torizon and write your application in C/C++, C#/.NET or Python.

You can also use other programming languages, or even ready-to-use technologies such as databases, by either choosing a container image from the community as the official Node.js container image or building your own container based on a community one or our Debian Containers for Torizon.

FAQ