How to create and debug Python project on a remote target

This document will show How to Create a Python Project and Run/Debug the Python Project. Before you create and debug a Python project, first install a Python plugin by following the How to Install Timesys external plugins document.

To develop and debug a Python application in TimeStorm

  1. Create a Python Project by adding Project Type, Grammar version, and Interpreter.
  2. Create a new Python Package under the project and add the module to the Package by selecting any one of the predefined templates.
  3. Set up Run/Debug configuration for a Remote target.
  4. Start run/debug the Python project.

Create Python Project:

To create a Python Project, use the PyDev wizard by clicking on the new →Other → PyDev → PyDev Project as shown below.

In the PyDev wizard,

  1. Project name: Enter the project name in the ‘Project name’ field.
  2. Project contents: Where the project should be located in the filesystem (can be pointed to existing sources). You can point other directories by unchecking the Use default and point the directory location by clicking on the browse button.
  3. Project type: Defines the kind of interpreter to be used for that project. Project type can be:
  • Python: The default implementation of the Python programming language is Cpython. As the name suggests Cpython is written in the C language. Cpython compiles the python source code into intermediate bytecode, which is executed by the Cpython virtual machine. CPython is distributed with a large standard library written in a mixture of C and Python. CPython provides the highest level of compatibility with Python packages and C extension modules. All versions of the Python language are implemented in C because CPython is the reference implementation. For more information, please refer to https://wiki.python.org/moin/PythonImplementations
  • Jython: Jython is an implementation of the Python programming language that can run on the Java platform. Jython programs use Java classes instead of Python modules. Jython compiles into Java byte code, which can then be run by a Java virtual machine. Jython enables the use of Java class library functions from the Python program. Jython is slow as compared to Cpython and lacks compatibility with CPython libraries. For more information, please visit https://www.jython.org/
  • IronPython: IronPython is an implementation of Python for the .NET framework. It can use both Python and .NET framework libraries, and can also expose Python code to other languages in the .NET framework. For more information, please visit https://wiki.python.org/moin/IronPython
  • Grammar version: A grammar is a formal description of a language that can be used to recognize its structure. In simple terms is a list of rules that define how each construct can be composed. For example, a rule for an if statement could specify that it must start with the “if” keyword, followed by a left parenthesis, an expression, a right parenthesis and a statement. The grammar verification may vary from version to version. Select the grammar version from the drop-down. You can use a newer interpreter while having the grammar you use compatible with an older interpreter (e.g.: using a Python 2.6 interpreter with a Python 2.4 grammar in the project).
  • Interpreter: An interpreter is a program which also converts a high-level Python code into machine code. In the Interpreter dropdown select which version of Interpreter to use for your code. If the Interpreter dropdown is empty, click on ‘Click here to configure an Interpreter not listed’ and configure the Interpreter by clicking on the Quick/Advanced Auto-Config.
  • Additional syntax validation: An additional syntax validation is optional. Here you can add additional syntaxes to which the sources of this project should be validated (so, if a project should be Python 2 and Python 3 compatible, you could select the main grammar as Python 3 and an additional syntax validation as Python 2).
  • PYTHONPATH configuration: PYTHONPATH configuration is optional. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run.
    • Add project directory to the PYTHONPATH: If you want to add your project to PYTHONPATH, select the Add project directory to the PYTHONPATH radio button.
    • Create src folder to your project and add it to the PYTHONPATH: To create a src folder to your project and add the project directory to the PYTHONPATH, select the ‘Create src folder to your project and add it to the PYTHONPATH’ radio button.
    • Create links to existing sources: To create links to existing sources, select Create links to existing sources (select them on the next page) radio button.
    • Select Don’t configure PYTHONPATH: If you don't want to add the project directory to the PYTHONPATH, select Don’t configure PYTHONPATH (to be done manually later on) radio button.
  • You may Finish on this screen or go to the Next where you will be asked which projects are referenced from this one (or have an additional step to create links to existing sources) which is optional.
  • Find the reference screenshot below.

    If you choose Next the Reference Project page will be shown as below.

    Finish button to create the PyDev Project as shown below.

    Python Package

    A Package helps you to organize a large number of files in different folders and subfolders based on some criteria so that you can find and manage them easily. A Python Package is actually a folder containing one or more Python Module files.

    To create a Python Package in TimeStorm, right click on the project → New → PyDev Package which will open ‘Create a new Python Package’ wizard as shown below.

    Enter Package Name in the ‘Name’ field and click on the Finish button which will create the package (e.g: samplePackage) with __init__.py (File named __init__.py are used to mark directories on disk as a Python package directories) in the Project as shown below.

    Python Module

    A Python module allows you to logically organize your Python code which makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. A module can define functions, classes and variables and can also include runnable code. For more information, please refer to https://docs.python.org/3/tutorial/modules.html

    1. To create a Python Module in TimeStorm, right click on the project → New → PyDev Module as shown below.
    2. To illustrate, the PyDev Module is selected which will open ‘Create a new Python Module’ with Source Folder, Package, Name fields as shown below.
    3. The ‘Source Folder’ and ‘Package’ fields will auto-fill. If not, click on browse buttons and point the Source Folder location and specify the Package name. Enter the Module name in the ‘Name’ field and click on the ‘Finish’ button which will display the list of predefined Templates as shown below.
    • CLI (argparse): The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. For more information, please refer to https://docs.python.org/3/library/argparse.html
    • CLI (optparse): Optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser, populate it with options, and parse the command line. For more information, please refer to the https://docs.python.org/2/library/optparse.html
    • Class: Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects. For more information, please refer to the https://docs.python.org/3/tutorial/classes.html
    • Main: Python main function is a starting point of any program. When the program is run, the python interpreter runs the code sequentially. Main function is executed only when it is run as a Python program. It will not run the main function if it is imported as a module. For more information, please refer to https://docs.python.org/3/library/__main__.html
    • Unittest: The unittest module provides a rich set of tools for constructing and running tests. For more information, please refer to https://docs.python.org/2/library/unittest.html
    • Unittest setup and teardown: The unittest module provides a rich set of tools for constructing and running tests. The setUp() and tearDown() methods allow you to define instructions that will be executed before and after each test method. For more information, please refer to https://docs.python.org/3/library/unittest.html#module-unittest

  • To illustrate, Module:Main is selected which will create the main module template in the Project as shown below.
  • In the template, add the additional code to the template and save the file. To illustrate, add simple code as shown below.
  • Set Up Python Application on a Remote Target

    Setup Python Project on a Remote Target with Run/Debug Configuration. The Configuration is a collection of settings that are required to download and run/debug an application on target. The run/debug configuration is persistent, and can be launched multiple times to run/debug the application. To run/debug the application on the remote target, you have to create a run/debug configuration.

    To create a run/debug configuration, right click on the project → Run As/Debug As → Run/Debug Configurations →TimeStorm Python Application Remote. It will open a Run/Debug configuration wizard with Main, Arguments, Environment, Interpreter, Target, Common tabs as shown below.

    1. Main Tab: In the Main tab, point the Python Project and Main Module by clicking on the Browse buttons. The ‘PYTHONPATH that will be used in the run’ will auto-fill as shown below.
    2. Arguments tab:In the Arguments tab, you can specify the Program/VM arguments to be passed to the application program/VM. The working directory is set as default, which means that the directory specified in the target definition is used as the working directory.
    3. Target tab:The Target tab, allows you to select the hardware target used when you run the application.

      In the Target tab, use the drop-down list to select a hardware target that you have already created. If you have not yet created a hardware target or want to change the settings for an existing target, click ‘Manage targets’ to open the Hardware Targets management utility.

    4. Interpreter tab:The Interpreter tab, allows you to select the Interpreter and use it when you run the application. Select the interpreter from the ‘Interpreter’ dropdown menu.

    5. Environment tab:In the ‘Environment’ tab, you can set environment variables for the target

      The panel shown below contains the following buttons:

      New — Click the ‘New’ button to create a new entry, and the ‘New Environment Variable’ dialog will appear, as shown below. Click the ‘Variables’ button to select the variables that you want to use. Then, click ‘OK’ twice.

      Select — Click the ‘Select‘ button to import environment variables from the host file system.

      Edit — To change an entry, select it from the list, and click the ‘Edit’ button.

      Remove — To delete an entry, select it from the list, and click the ‘Remove’ button.

    6. Common tab: The ‘Common’ tab, sets options for sharing this run configuration among multiple projects.

      The options with the Common tab include:

      • Local File — By default, run configurations are saved with the workspace state files, so they can only be used with projects in the current workspace. (This setting corresponds to the ‘Local file’ radio button on this panel.) However, you can make the configuration available for use in other workspaces by choosing the ‘Shared file’ radio button.
      • Shared File — When you select this option, the run configuration is saved as a .launch file that can be imported into another TimeStorm workspace. Optionally, you can specify a different location for the file so that it is more easily accessible to multiple projects.
      • Display in Favorites Menu — You can select whether to include this run configuration on the main ‘Run’ and ‘Debug’ menus. For example, if you select the ‘Run’ checkbox, the run configuration always appears in the ‘Run History’ submenu.

      You can choose to include the run configuration in either menu, both menus, or neither menu

      • Launch in Background — Selecting the ‘Launch in background’ checkbox causes this run configuration to run as a background thread. Running as a background thread is the default value. Clear this checkbox if you want to run in the foreground, along with other TimeStorm processing.

    Run Python Application on A Remote Target

    After setting the values in all the tabs by following the “Set Up Python Application on a Remote Target” section, click the Run button at the bottom of the run configuration dialog. This will connect your host to your target, download the application to the target and execute the application on the target. The commands executed on the target and the program output progress are displayed in the console view as shown below.

    Debug Python application on a Remote Target:

    To debug a Python application, right click on the project and click on Debug As → Debug Configurations → TimeStorm Python Application Remote in the project context menu. This will open a Debug configuration with Main, Arguments, Environment, Interpreter, Target, Common tabs as shown below.

    After setting the values in all the tabs by following the “Set Up Python Application on a Remote Target” section. Click the Debug button at the bottom of the Debug configuration dialog. This will connect your host to your target, download the application to the target and start debugging. By default the debugger will stop at the main function and you can continue debugging from there.

    Stepping commands: The TimeStorm Platform helps developers debug by providing buttons in the toolbar and key binding shortcuts to control program execution.