Python scripting - Introduction

Debugging scripts with Visual Studio Code

 

Visual Studio Code


Visual Studio Code is a source-code editor made by Microsoft for Windows, Linux and macOS.

Features include the support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git.

 

VS Code can be downloaded from the official website: https://code.visualstudio.com/download

 


Recommended VS Code extensions


1.Python (Pylance included) - a must-have extension that includes features such as IntelliSense (Pylance), linting, debugging, code navigation, code formatting, refactoring, variable explorer, test explorer, and more.

2.Code Spell Checker - a must-have basic spell checker.

3.autoDocstring - Python Docstring Generator - to quickly generate docstrings for python functions and classes.

4.Indent-Rainbow - a simple extension to make indentation more readable.

5.Python Indent - corrects Python indentation.


Configure the debugger


1

Configure FASTSUITE Edition 2

1.1

Search for the file cenpyinit.py in the root folder of FASTSUITE and open it in a editor.

 

1.2

Enable the debugging by setting the flag:  

enablePythonDebug = True

 


2

Configure Visual Studio Code

2.1

Make sure that Visual Studio Code has been installed properly

 

2.2

Run Visual Studio Code.

Verify that the Python extension has been installed. The extension can be found here: VS Marketplace

 

vsCodePy

 

2.3

Configure the Workspace of VSCode.

Add the Scripts folder(s) where to find the Python files that are to be debugged.

 

vsCodeProj

 

2.4

Open the Run and Debug menu and start the Add Configuration... command to add the debug configuration launch.json file.

 

vsCodeDbgConfig0

 

The launch.json file will be created. Copy and paste the code below:

 

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "E2 Python Attach",

            "type": "python",

            "request": "attach",

            "justMyCode":false,

            "port": 5254,

            "host": "localhost",

            "pathMappings": [

                {                   

                    "localRoot": "${workspaceFolder}",

                    "remoteRoot": "${workspaceFolder}"

                }

            ]

        }

    ]

}

 

vsCodeDbgConfig1

2.5

IntelliSense settings

 

Call Command Palette (Ctrl+Shift+P), write "workspace settings json" and press enter.

WorkspaceSettingsJSON

 

The new settings.json file will be added to .vscode folder inside your workspace. Copy and paste the code below:

 

{

   // For IntelliSense to work, uncomment the line below and set the interpreter path to the E2 installation folder

   //"python.defaultInterpreterPath": "E2InstallationPath\\python.exe",

   "files.autoSave": "onFocusChange",

   "python.linting.enabled": true,

   "editor.tabSize": 3,

   "editor.detectIndentation": false,

   "editor.renderWhitespace": "boundary"

}

 

Uncomment the specified line and replace the local E2 installation path. The default interpreter path should point to the Python executable from the desired E2 release:

 

WorkspaceSettingsJSONFile

 

As a result, the IntelliSense should be active:

 

IntelliSenseExample

 


3

Debugging

3.1

Start FASTSUITE Edition 2 and load your scenario with the Python script(s).

 

3.2

If Python debugging is running for the first time, administrator permission will be requested.

Wait until the ptvsd message appears and then click the OK button.

 

vsCodeDbgWindow

 

Now the debugger is ready and can be attached.

 

Information

It is only possible to attach the debugger after E2 runs a Python script for the first time.

 

3.3

Go to the Run and Debug menu in VSCode and press Start Debugging or use the F5 key on the keyboard.

The debug is active and the breakpoints in the Python script can be seen.

 

vsCodeDbg



Previous
Page précédente
Chapter
Page principale du chapitre
Next
Page suivante