Python Executor Setup
The Trendz Python Executor is a Docker-based microservice required for:
- Python Calculation fields
- All prediction models (except Fourier Transformation)
- Code generation for Metric Explorer
Deployment recommendations:
- Same host — deploy the Python Executor on the same host as Trendz. Communication uses WebSockets; placing them on separate hosts requires opening the executor port in the firewall and increases latency.
- No public access — never expose the Python Executor port to the internet. Only Trendz needs to reach it.
- Resources — allocate at least 1 GB RAM and 1 CPU core. More CPU directly speeds up prediction model training and execution.
Prerequisites
Section titled “Prerequisites”- Docker CE and Docker Compose installed.
- Docker Desktop for Windows installed.
Standalone installation
Section titled “Standalone installation”Use these steps only if you installed Trendz via a package (Ubuntu, RHEL/CentOS, or Windows) and need to add the Python Executor separately.
Step 1. Create Docker Compose file
Section titled “Step 1. Create Docker Compose file”Create a new docker-compose.yml with the following content:
services: trendz-python-executor: restart: always image: "thingsboard/trendz-python-executor:1.16.1.3" ports: - "127.0.0.1:8181:8181" environment: EXECUTOR_MANAGER: 1 EXECUTOR_SCRIPT_ENGINE: 6 THROTTLING_QUEUE_CAPACITY: 10 THROTTLING_THREAD_POOL_SIZE: 6 NETWORK_BUFFER_SIZE: 5242880 volumes: - trendz-python-executor-conf:/python-executor-config-files - trendz-python-executor-data:/datavolumes: trendz-python-executor-conf: name: trendz-python-executor-conf driver: local trendz-python-executor-data: name: trendz-python-executor-data driver: localDocker Compose parameters:
| Parameter | Description |
|---|---|
127.0.0.1:8181:8181 |
Publishes the Python Executor port on the local host only. If Trendz runs on another host, publish it on an internal network address instead, never on a public one |
restart: always |
Auto-restarts the container on failure or system reboot |
EXECUTOR_MANAGER |
Number of executor manager threads |
EXECUTOR_SCRIPT_ENGINE |
Number of script engine threads |
THROTTLING_QUEUE_CAPACITY |
Maximum number of queued script executions |
THROTTLING_THREAD_POOL_SIZE |
Number of threads for throttling |
NETWORK_BUFFER_SIZE |
WebSocket network buffer size in bytes |
trendz-python-executor-conf:/python-executor-config-files |
Persists Python Executor configuration files |
trendz-python-executor-data:/data |
Persists Python Executor data directory |
Step 2. Start the Python Executor
Section titled “Step 2. Start the Python Executor”Run from the directory containing your docker-compose.yml:
docker compose up -ddocker compose logs -f trendz-python-executorStep 3. Connect Trendz to the Python Executor
Section titled “Step 3. Connect Trendz to the Python Executor”Edit /usr/share/trendz/conf/trendz.conf and add:
export SCRIPT_ENGINE_TIMEOUT=30000export SCRIPT_ENGINE_PROVIDER=DOCKER_CONTAINERexport SCRIPT_ENGINE_DOCKER_PROVIDER_URL=PYTHON_EXECUTOR_HOST:PYTHON_EXECUTOR_PORTReplace PYTHON_EXECUTOR_HOST and PYTHON_EXECUTOR_PORT with the actual host and port where your Python Executor is running. Ensure Trendz can reach this network destination.
Restart Trendz to apply the changes:
sudo systemctl restart trendzOpen Notepad as Administrator and edit:
C:\Program Files (x86)\trendz\conf\trendz.ymlLocate the script-engine block and update:
script-engine: provider: "${SCRIPT_ENGINE_PROVIDER:DOCKER_CONTAINER}" runtime-timeout: "${SCRIPT_ENGINE_TIMEOUT:30000}" callback-timeout: "${SCRIPT_ENGINE_TIMEOUT:30000}" docker-provider-url: "${SCRIPT_ENGINE_DOCKER_PROVIDER_URL:PYTHON_EXECUTOR_HOST:PYTHON_EXECUTOR_PORT}" websocket-buffer-size: "${SCRIPT_ENGINE_WEBSOCKET_BUFFER_SIZE:20971520}" websocket-concurrency: "${SCRIPT_ENGINE_WEBSOCKET_CONCURRENCY:5}"Replace PYTHON_EXECUTOR_HOST and PYTHON_EXECUTOR_PORT with the actual host and port where your Python Executor is running. Ensure Trendz can reach this network destination.
Restart the Trendz service:
net stop trendznet start trendzStep 4. Replace the default shared secret
Section titled “Step 4. Replace the default shared secret”Trendz authenticates to the Python Executor with a shared secret. Trendz and the executor both default to the publicly known value placeholder, so replace it with your own: generate a random value, for example with openssl rand -hex 32, and set the same value on both sides.
In the Python Executor docker-compose.yml, add it under environment::
PYTHON_EXECUTOR_SECRET: YOUR_SECRETAdd to /usr/share/trendz/conf/trendz.conf:
export PYTHON_EXECUTOR_SECRET=YOUR_SECRETIn C:\Program Files (x86)\trendz\conf\trendz.yml, set the secret in the script-engine block:
executor-secret: "${PYTHON_EXECUTOR_SECRET:YOUR_SECRET}"Apply the change: in the Python Executor folder, run docker compose up -d (a plain restart keeps the old environment), then restart Trendz. If the values differ, Trendz cannot run Python scripts.
Replace the shared secret in Docker and Kubernetes installations
Section titled “Replace the shared secret in Docker and Kubernetes installations”The Python Executor that comes with a Docker or Kubernetes installation also uses the default secret placeholder. Generate a random value, for example with openssl rand -hex 32, and set it on both services:
-
Docker: add
PYTHON_EXECUTOR_SECRET: YOUR_SECRETunderenvironment:of both thetrendzandtrendz-python-executorservices indocker-compose.yml, then rundocker compose up -d. If Trendz runs alongside ThingsBoard, the services are indocker-compose-trendz.yml; apply withdocker compose -f docker-compose.yml -f docker-compose-trendz.yml up -d. -
Kubernetes: replace
placeholderintrendz-python-executor-secret.ymland apply it. Trendz and the Python Executor read this Secret only at start, so restart them:Terminal window kubectl apply -f trendz-python-executor-secret.ymlkubectl -n trendz rollout restart deployment/trendz-python-executor-deployment statefulset/trendz-appThe StatefulSet restarts the Trendz pods one at a time. With one pod, Trendz is unavailable until it restarts.
Add custom Python libraries
Section titled “Add custom Python libraries”You can extend the Python Executor with additional libraries to use in Calculation Fields or Custom Prediction Models.
The following libraries are bundled in the official Docker image and available out of the box:
| Library | Min version |
|---|---|
numpy |
2.0 |
pandas |
2.1.0 |
scikit-learn |
1.6.0 |
statsmodels |
0.14.0 |
matplotlib |
3.7.0 |
prophet |
1.2.0 |
xgboost-cpu |
2.1.0 |
requests |
2.31.0 |
The executor installs other libraries from the requirements.txt file in its configuration folder, at every start, when
INSTALL_PYTHON_DEPS_ON_STARTUP is true. This is off by default. When it is on:
- The executor downloads pip and the libraries from PyPI (
files.pythonhosted.org,pypi.org) at every start, so it needs outbound internet access. - The image has no compiler: a library with compiled code must be available as a binary wheel for Python 3.13 on Linux. Pure Python libraries install from source too.
- If a library fails to install, the executor does not start. Check its log.
For example, to add the emoji library at version 2.2.0:
If Trendz runs alongside ThingsBoard, the trendz-python-executor service is in docker-compose-trendz.yml: edit that
file and run the commands below as docker compose -f docker-compose.yml -f docker-compose-trendz.yml ....
-
Add the library to
requirements.txtin the executor configuration folder. Run from the directory containing yourdocker-compose.yml:Terminal window docker compose exec trendz-python-executor sh -c "echo 'emoji==2.2.0' >> /python-executor-config-files/requirements.txt" -
In
docker-compose.yml, add to theenvironment:of thetrendz-python-executorservice:INSTALL_PYTHON_DEPS_ON_STARTUP: "true" -
Recreate the container so that it gets the new environment:
Terminal window docker compose up -d trendz-python-executor -
Check the executor log:
Terminal window docker compose logs trendz-python-executor
-
In
trendz-python-executor-files-config.yml, list the libraries you add underrequirements.txt. The bundled libraries are already in the image, so replace the list that is there with yours only:requirements.txt: |emoji==2.2.0 -
In
trendz-python-executor-env-config.yml, add underdata::INSTALL_PYTHON_DEPS_ON_STARTUP: "true" -
The network policy in
trendz-python-executor-network-policy.ymlblocks all outbound traffic from the executor. Replaceegress: []with rules that allow DNS and HTTPS:egress:- to:- namespaceSelector: {}podSelector:matchLabels:k8s-app: kube-dnsports:- protocol: UDPport: 53- protocol: TCPport: 53- ports:- protocol: TCPport: 443This also lets Python scripts reach any HTTPS address. To limit it, allow only the IP ranges of your PyPI mirror with an
ipBlock. -
Apply the files and restart the executor:
Terminal window kubectl apply -f trendz-python-executor-files-config.yml -f trendz-python-executor-env-config.ymlkubectl apply -f trendz-python-executor-network-policy.ymlkubectl -n trendz rollout restart deployment/trendz-python-executor-deployment -
Check the executor log:
Terminal window kubectl -n trendz logs deployment/trendz-python-executor-deployment
The log shows the installation:
Installing Python dependencies from /home/python-executor/configs/requirements.txt (startup mode enabled)......Successfully installed emoji-2.2.0The library is now available in your Trendz Calculation Fields and Custom Prediction Models.
Configuration properties
Section titled “Configuration properties”All Python Executor settings are configured via environment variables. Pass them under the environment: key in your docker-compose.yml.
| Environment variable | Default | Description |
|---|---|---|
EXECUTOR_MANAGER |
1 |
Number of manager executor threads |
EXECUTOR_SCRIPT_ENGINE |
6 |
Number of script engine threads. Increase for higher parallel script execution throughput |
THROTTLING_QUEUE_CAPACITY |
10 |
Maximum number of script execution requests that can wait in queue. Requests beyond this limit are rejected |
NETWORK_BUFFER_SIZE |
20971520 |
WebSocket message buffer size in bytes (default 20 MB). Increase when scripts transfer large datasets |
INSTALL_PYTHON_DEPS_ON_STARTUP |
false |
Install the libraries listed in requirements.txt at every start; see Add custom Python libraries |
PYTHON_EXECUTOR_SECRET |
placeholder |
Shared secret Trendz uses to authenticate to the Python Executor. Replace the default and set the same value in Trendz |
SCRIPT_ENGINE_RUNTIME_TIMEOUT |
10000 |
Python script execution timeout in milliseconds. Scripts that exceed this limit are terminated |
Verify the connection
Section titled “Verify the connection”To confirm that Trendz is successfully connected to the Python Executor, create a simple Python Calculation Field and run it — for example:
return 42If the script executes and returns a result, the connection is working. If it times out or returns an error, check
that the Python Executor is running and that SCRIPT_ENGINE_DOCKER_PROVIDER_URL points to the correct host and port.
Was this helpful?