Python and .NET.

TutorialsPython and .NET Integration | Tutorial | Reference


This Tutorial Teaches you to:

  • Install Python
  • Configure Python
  • Using Python in the solution

Step 1: Installation and Setup

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


  • You can download Python here.
  • Check the User admin privileges checkbox, and select Customize 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 when deploying the solution for production.

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

  • Add Python.exe to PATH

Especially 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 the 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 start using Python in your solution.

Note: For Python versions above 3.12, Pythonnet is not available. For more information, visit their documentation.

  • Go to Solution / Settings and locate the Python Interpreter box. Click the "..." button, navigate to find the installed Python Engine, and select the python.exe file.

Python Interpreter

Local

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

Server

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

Step 2: Using Python in the Solution


Scripts Tasks and Classes

  • Scripts / Tasks: Event driven or periodical tasks, running on the server computer. They can be written in Python, C# or VB.NET.
  • Scripts / Classes: library of methods and functions that can be used on both server and client machines. They can be written in Python, C# or VB.NET.

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 CodeBehind you can call tasks and methods, which can be written in Python. 

2.1 Shell Integration

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

In order to achieve that, follow the steps below:

  1. Go to your solution folder (the folder where your solution is located) and add a script with the name "ExternalSum.py"
  2. In this file, add the following code:
  3. ExternalSum
    import sys
    
    value1 = float(sys.argv[1])
    value2 = float(sys.argv[2])
    result = value1 + value2
    print(result)
  4. Create a Script → Task and choose Python as the language
  5. Add a trigger task into the Trigger column
  6. Create the following tags:
    1. Tag1 - Integer
    2. Tag2 - Integer
    3. TagResult - Integer
  7. In your task, use the following script:
  8. ExternalSum
    arg1 = @Tag.Tag1(0)
    arg2 = @Tag.Tag2(0)
    result = TK.ExecutePythonShell("_ExecutionPath_ExternalSum.py", [arg1, arg2])
    @Tag.Result(0) = result
  9. Run your solution and use Property Watch to change the values and see the results (don't forget to run the task trigger)


2.2 Standard Integration

  • Standard Integration allows you to execute Python code in FrameworX script task.

In order to achieve that, follow the steps below:

  1. Create a Script → Task and choose Python as the language
  2. Add a trigger task into the Trigger column
  3. Create the following tags:
    1. Tag1 - Integer
    2. Tag2 - Integer
    3. TagResult - Integer
  4. In your task, use the following script:
  5. Internal Sum
    def add(val1, val2):
        return val1 + val2
  6. Run your solution and use Property Watch to change the values and see the results (don't forget to run the task trigger)

2.3 Python and .NET Integration

  • Python and .NET Integration allows a Python function directly from Python Class on the code editor.

In order to achieve that, follow the steps below:

  1. Create a Script → Task and choose CSharp as the language
  2. Add a trigger task into the Trigger column
  3. Create the following tags:
    1. Tag1 - Integer
    2. Tag2 - Integer
    3. TagResult - Integer
  4. In your task, use the following script:
  5. Python and .NET
    @Tag.Result = TK.ToInt( @Script.Class.ClassPython.sum(@Tag.Tag1, @Tag.Tag2) );
  6. Run your solution and use Property Watch to change the values and see the results (don't forget to run the task trigger)




Explanation - to understand concepts

Platform / Technology Foundation / Python and .NET Integration

Tutorials - to learn by doing

Tutorials /  Technology Learning / Python and .NET Integration

Reference - technical details

→ Technical Reference / Programming and APIs Reference / Python and .NET Integration


In this section...