yaml
type: "io.kestra.plugin.scripts.python.Commands"

Execute one or more Python scripts from a Command Line Interface.

Examples

Execute a Python script in a Conda virtual environment. First, add the following script in the embedded Code Editor and name it etl_script.py:

python
import argparse

parser = argparse.ArgumentParser()

parser.add_argument("--num", type=int, default=42, help="Enter an integer")

args = parser.parse_args()
result = args.num * 2
print(result)

Then, make sure to set the enabled flag of the namespaceFiles property to true to enable namespace files. We include only the etl_script.py file as that is the only file we require from namespace files.

This flow uses a io.kestra.plugin.core.runner.Process Task Runner and Conda virtual environment for process isolation and dependency management. However, note that, by default, Kestra runs tasks in a Docker container (i.e. a Docker task runner), and you can use the taskRunner property to customize many options, as well as containerImage to choose the Docker image to use.

yaml
id: python_venv
namespace: company.team

tasks:
  - id: python
    type: io.kestra.plugin.scripts.python.Commands
    namespaceFiles:
      enabled: true
      include:
        - etl_script.py
    taskRunner:
      type: io.kestra.plugin.core.runner.Process
    beforeCommands:
      - conda activate myCondaEnv
    commands:
      - python etl_script.py

Execute a Python script from Git in a Docker container and output a file

yaml
id: python_commands_example
namespace: company.team

tasks:
  - id: wdir
    type: io.kestra.plugin.core.flow.WorkingDirectory
    tasks:
      - id: clone_repository
        type: io.kestra.plugin.git.Clone
        url: https://github.com/kestra-io/examples
        branch: main

      - id: git_python_scripts
        type: io.kestra.plugin.scripts.python.Commands
        warningOnStdErr: false
        containerImage: ghcr.io/kestra-io/pydata:latest
        beforeCommands:
          - pip install faker > /dev/null
        commands:
          - python examples/scripts/etl_script.py
          - python examples/scripts/generate_orders.py
        outputFiles:
          - orders.csv

  - id: load_csv_to_s3
    type: io.kestra.plugin.aws.s3.Upload
    accessKeyId: "{{ secret('AWS_ACCESS_KEY_ID') }}"
    secretKeyId: "{{ secret('AWS_SECRET_ACCESS_KEY') }}"
    region: eu-central-1
    bucket: kestraio
    key: stage/orders.csv
    from: "{{ outputs.gitPythonScripts.outputFiles['orders.csv'] }}"

Execute a Python script on a remote worker with a GPU

yaml
id: gpu_task
namespace: company.team

tasks:
  - id: python
    type: io.kestra.plugin.scripts.python.Commands
    taskRunner:
      type: io.kestra.plugin.core.runner.Process
    commands:
      - python ml_on_gpu.py
    workerGroup:
      key: gpu

Pass detected S3 objects from the event trigger to a Python script

yaml
id: s3_trigger_commands
namespace: company.team
description: process CSV file from S3 trigger

tasks:
  - id: wdir
    type: io.kestra.plugin.core.flow.WorkingDirectory
    tasks:
      - id: clone_repository
        type: io.kestra.plugin.git.Clone
        url: https://github.com/kestra-io/examples
        branch: main

      - id: python
        type: io.kestra.plugin.scripts.python.Commands
        inputFiles:
          data.csv: "{{ trigger.objects | jq('.[].uri') | first }}"
        description: this script reads a file `data.csv` from S3 trigger
        containerImage: ghcr.io/kestra-io/pydata:latest
        warningOnStdErr: false
        commands:
          - python examples/scripts/clean_messy_dataset.py
        outputFiles:
          - "*.csv"
          - "*.parquet"

triggers:
  - id: wait_for_s3_object
    type: io.kestra.plugin.aws.s3.Trigger
    bucket: declarative-orchestration
    maxKeys: 1
    interval: PT1S
    filter: FILES
    action: MOVE
    prefix: raw/
    moveTo:
      key: archive/raw/
    accessKeyId: "{{ secret('AWS_ACCESS_KEY_ID') }}"
    secretKeyId: "{{ secret('AWS_SECRET_ACCESS_KEY') }}"
    region: "{{ secret('AWS_DEFAULT_REGION') }}"

Execute a Python script from Git using a private Docker container image

yaml
id: python_in_container
namespace: company.team

tasks:
  - id: wdir
    type: io.kestra.plugin.core.flow.WorkingDirectory
    tasks:
      - id: clone_repository
        type: io.kestra.plugin.git.Clone
        url: https://github.com/kestra-io/examples
        branch: main

      - id: git_python_scripts
        type: io.kestra.plugin.scripts.python.Commands
        warningOnStdErr: false
        commands:
          - python examples/scripts/etl_script.py
        outputFiles:
          - "*.csv"
          - "*.parquet"
        containerImage: annageller/kestra:latest
        taskRunner:
          type: io.kestra.plugin.scripts.runner.docker.Docker
          config: |
            {
              "auths": {
                  "https://index.docker.io/v1/": {
                      "username": "annageller",
                      "password": "{{ secret('DOCKER_PAT') }}"
                  }
              }
            }

Create a python script and execute it in a virtual environment

yaml
id: script_in_venv
namespace: company.team
tasks:
  - id: python
    type: io.kestra.plugin.scripts.python.Commands
    inputFiles:
      main.py: |
        import requests
        from kestra import Kestra

        response = requests.get('https://google.com')
        print(response.status_code)
        Kestra.outputs({'status': response.status_code, 'text': response.text})
    beforeCommands:
      - python -m venv venv
      - . venv/bin/activate
      - pip install requests kestra > /dev/null
    commands:
      - python main.py

Properties

commands

  • Type: array
  • SubType: string
  • Dynamic: ✔️
  • Required: ✔️
  • Min items: 1

The commands to run.

interpreter

  • Type: array
  • SubType: string
  • Dynamic:
  • Required: ✔️
  • Default: [ "/bin/sh", "-c" ]
  • Min items: 1

Which interpreter to use.

warningOnStdErr

  • Type: boolean
  • Dynamic:
  • Required: ✔️
  • Default: true

Whether to set the task state to WARNING when any stdErr output is detected.

Note that a script error will set the state to FAILED regardless.

beforeCommands

  • Type: array
  • SubType: string
  • Dynamic: ✔️
  • Required:

A list of commands that will run before the commands, allowing to set up the environment e.g. pip install -r requirements.txt.

containerImage

  • Type: string
  • Dynamic: ✔️
  • Required:
  • Default: ghcr.io/kestra-io/kestrapy:latest

The task runner container image, only used if the task runner is container-based.

docker

Deprecated - use the 'taskRunner' property instead.

Only used if the taskRunner property is not set

env

  • Type: object
  • SubType: string
  • Dynamic: ✔️
  • Required:

Additional environment variables for the current process.

failFast

  • Type: boolean
  • Dynamic:
  • Required:
  • Default: true

Fail the task on the first command with a non-zero status.

If set to false all commands will be executed one after the other. The final state of task execution is determined by the last command. Note that this property maybe be ignored if a non compatible interpreter is specified. You can also disable it if your interpreter does not support the set -eoption.

inputFiles

  • Type:
    • object
    • string
  • Dynamic: ✔️
  • Required:

The files to create on the local filesystem. It can be a map or a JSON object.

namespaceFiles

Inject namespace files.

Inject namespace files to this task. When enabled, it will, by default, load all namespace files into the working directory. However, you can use the include or exclude properties to limit which namespace files will be injected.

outputDirectory

  • Type: boolean
  • Dynamic:
  • Required:
  • Default: false

Whether to setup the output directory mechanism.

Required to use the expression. Note that it could increase the starting time. Deprecated, use the outputFiles property instead.

outputFiles

  • Type: array
  • SubType: string
  • Dynamic: ✔️
  • Required:

The files from the local filesystem to send to Kestra's internal storage.

Must be a list of glob) expressions relative to the current working directory, some examples: my-dir/, my-dir/*/ or my-dir/my-file.txt.

runner

  • Type: string
  • Dynamic:
  • Required:
  • Possible Values:
    • PROCESS
    • DOCKER

Deprecated - use the 'taskRunner' property instead.

Only used if the taskRunner property is not set

targetOS

  • Type: string
  • Dynamic:
  • Required:
  • Default: AUTO
  • Possible Values:
    • LINUX
    • WINDOWS
    • AUTO

The target operating system where the script will run.

taskRunner

  • Type: TaskRunner
  • Dynamic:
  • Required:
  • Default: { "type": "io.kestra.plugin.scripts.runner.docker.Docker" }

The task runner to use.

Task runners are provided by plugins, each have their own properties.

Outputs

exitCode

  • Type: integer
  • Required: ✔️
  • Default: 0

outputFiles

  • Type: object
  • SubType: string
  • Required:

vars

  • Type: object
  • Required:

Definitions

io.kestra.core.models.tasks.NamespaceFiles

  • enabled
    • Type: boolean
    • Dynamic:
    • Required:
    • Default: true
  • exclude
    • Type: array
    • SubType: string
    • Dynamic:
    • Required:
  • include
    • Type: array
    • SubType: string
    • Dynamic:
    • Required:

io.kestra.plugin.scripts.runner.docker.Cpu

  • cpus
    • Type: integer
    • Dynamic:
    • Required:

io.kestra.core.models.tasks.runners.TaskRunner

  • type
    • Type: string
    • Dynamic:
    • Required: ✔️
    • Validation RegExp: \p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*(\.\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*)*
    • Min length: 1

io.kestra.plugin.scripts.runner.docker.Memory

  • kernelMemory
    • Type: string
    • Dynamic: ✔️
    • Required:
  • memory
    • Type: string
    • Dynamic: ✔️
    • Required:
  • memoryReservation
    • Type: string
    • Dynamic: ✔️
    • Required:
  • memorySwap
    • Type: string
    • Dynamic: ✔️
    • Required:
  • memorySwappiness
    • Type: string
    • Dynamic: ✔️
    • Required:
  • oomKillDisable
    • Type: boolean
    • Dynamic:
    • Required:

io.kestra.plugin.scripts.exec.scripts.models.DockerOptions

  • image
    • Type: string
    • Dynamic: ✔️
    • Required: ✔️
    • Min length: 1
  • config
    • Type:
      • string
      • object
    • Dynamic: ✔️
    • Required:
  • cpu
    • Type: Cpu
    • Dynamic:
    • Required:
  • credentials
  • deviceRequests
  • entryPoint
    • Type: array
    • SubType: string
    • Dynamic: ✔️
    • Required:
  • extraHosts
    • Type: array
    • SubType: string
    • Dynamic: ✔️
    • Required:
  • host
    • Type: string
    • Dynamic: ✔️
    • Required:
  • memory
    • Type: Memory
    • Dynamic:
    • Required:
  • networkMode
    • Type: string
    • Dynamic: ✔️
    • Required:
  • pullPolicy
    • Type: string
    • Dynamic:
    • Required:
    • Default: ALWAYS
    • Possible Values:
      • IF_NOT_PRESENT
      • ALWAYS
      • NEVER
  • shmSize
    • Type: string
    • Dynamic: ✔️
    • Required:
  • user
    • Type: string
    • Dynamic: ✔️
    • Required:
  • volumes
    • Type: array
    • SubType: string
    • Dynamic: ✔️
    • Required:

io.kestra.plugin.scripts.runner.docker.Credentials

  • auth
    • Type: string
    • Dynamic: ✔️
    • Required:
  • identityToken
    • Type: string
    • Dynamic: ✔️
    • Required:
  • password
    • Type: string
    • Dynamic: ✔️
    • Required:
  • registry
    • Type: string
    • Dynamic: ✔️
    • Required:
  • registryToken
    • Type: string
    • Dynamic: ✔️
    • Required:
  • username
    • Type: string
    • Dynamic: ✔️
    • Required:

io.kestra.plugin.scripts.runner.docker.DeviceRequest

  • capabilities
    • Type: array
    • SubType: array
    • Dynamic:
    • Required:
  • count
    • Type: integer
    • Dynamic:
    • Required:
  • deviceIds
    • Type: array
    • SubType: string
    • Dynamic: ✔️
    • Required:
  • driver
    • Type: string
    • Dynamic: ✔️
    • Required:
  • options
    • Type: object
    • SubType: string
    • Dynamic:
    • Required: