Overview

The platform includes a unique set of features for Python integration:

  • Multiple Python Versions: Support for different Python versions.
  • In-Solution Designer Editing: Edit and test Python scripts within the Designer.
  • Tag Direct Access: Python can access real-time tags, Historian tables, and all modules' namespaces.
  • .NET Integration: Python can call methods in C# or VB.Net, and vice versa.
  • Web Integration: Web pages can use Python classes from the server.
  • Extensibility Toolkit: Allows external Python applications to interface with the platform's servers.

On this page:


Installation and Setup

The Python Shell integration requires Python releases 3.7 onwards and the installation of Python.NET.

Setup Steps:

    1.    Install Python and Python.NET in the machines where you run the application and on those using the Solution Designer.
    2.    Configure the Solution Settings to select the appropriate Python shell folder.

Each solution can specify its own Python interpreter, facilitating the development and maintenance of different solutions that may require different Python versions.

You can download Python here.

Check the Use Admin Privileges, and the option to Customize the installation.

During the installation of the Python Engine,select the option to “Install for all users”.

if You install python under your local user, you may have issues when running the solution as a service, or deployment the solution for production.

This image can be slightly different to your Python version, but always enable the options equivalent to:

  • Add Python.exe to PATH

Specially in the production server.

  • Install with admin rights to all users,
  • Customize the installation to installation in the Program Files, instead of Users folder


Once you've downloaded, open command prompt as an ADMINISTRATOR and type “pip install pythonnet”. For this work, you'll need internet access. If internet is not available, download and install manually.

Once Python .NET has been installed, you can then start using Python in your solution.

Go to Solution → Settings tab and locate for the Python GroupBox. Click the "..." button, navigate to find the installed Python Engine, and select the python.exe file.

Python Interpreter

Local

Defines the path for Python installation folder on the local machine.

Server

Defines the path for Python installation folder on the remote server.

Once you've installed Python,  you can take a look at our Python demo with code examples.

→ Learn more at Python Demo.


Using Python in the Solution

The Python Demo solution illustrates how to use the Python and .NET languages for the Scripts.

This section presents a summary of that information, listing the modules in the solution where Python code can be used.



Scripts Tasks and Classes

  • Script-Tasks: event driven or periodical tasks, running in the server computer, can be written in Python, C$ or VB.NET
  • Script Classes: library of methods and  functions that be used in both server and client machines. They can be written in Python, C# or VB.Net Code.

Displays CodeBehind and Graphical Elements 

The Code Behind for the displays, can be written in C#, VB.NET or JavaScript, not Python directly. But from the Code Behind you can call activate scripts tasks and methods, with can written in Python. 

Shell Integration

  • Shell Integration: allows to execute Python code from external files.

This integration allows to execute Python code from external file. Just create a Script.Task with the following code:

Shell Integration example
# This code call the execution of the external file using Python Shell 
# with the optional args defined in this initial section
#
# The macro _ExecutionPath_ is replaced by the path where the solution is set to execute
# Replace that macro by a specific path, or user other built-in macros as nedded
#

arg1 = @Tag.Tag1
arg2 = @Tag.Tag2
result = TK.ExecutePythonShell("_ExecutionPath_ExternalSum.py", [arg1, arg2])
@Tag.Result = result

Python Shell Version and Tests

When creating Script > Tasks, with this method, the Play Button in the top toolbar, will execute the Python code directing the printing output to the Designer Output Window.

As long as the Python.exe file is properly mapped on Solutions > Settings page, this integration works with any Python version.


Designer Python Code Editor

Code Editor User Interface

The code editor is accessed at the Designer Scripts → Code Editor page

Testing the code

When the editor is used to edit Script Tasks, you can test the scripts directly, using the Play button at the Task name line. The results of your code will show in the standard Designer output.

Accessing Tags and Modules Properties

The access to tags and other properties for other models is exactly the same as C# or VB.Net; just use the '@' symbol to access the properties directly, as shown in the image: '@tag.Tag1'.


.NET Interoperability

Calling ScriptClass Methods

At the Script or in any place in your solution where Expressions are allowed, you can use the following syntax to call classes.

@Script.Class.TestClass.ExempleMethod(param1, param2)

This activation is independent of the language the class was created in.

This means C# and VB.NET can call Python classes, and Python code can call .NET classes.


In this section:

  • No labels