Run Python Code
Controlled node
Overview
Execute Python code in a serverless container environment. This node allows you to run custom Python scripts with support for package installation, prebuild scripts, and structured input/output handling. The code executes in an isolated environment with access to a filesystem where inputs are provided via inputs.json and results are captured from output.json.
Execution Environment
- Language: Python 3.x
- Timeout: 10 minutes (600 seconds) maximum execution time
- Package Manager: pip (packages installed fresh per execution)
- Prebuild: Shell commands supported for environment setup
- Storage: Ephemeral filesystem with
inputs.jsonandoutput.jsonaccess
Input/Output Pattern
The node follows a file-based IO pattern:
- Read workflow inputs from
inputs.json(automatically populated from theinputssocket) - Write results to
output.json(automatically captured to theresultoutput) - Use
print()for logging (captured instdout)
Inputs
| Input | Type | Description | Default |
|---|---|---|---|
| Run | Event | Triggers the Python code execution | - |
| Code | Text | Python code to execute. Must read from inputs.json and write to output.json. | See default template below |
| Packages | Text | Comma-separated list of pip packages to install (e.g., requests==2.28.1, pandas). Can also accept an array of strings via input connection. | - |
| Prebuild Script | Text | Shell commands to run before code execution (e.g., apt-get update). Useful for system dependencies. | - |
| Inputs | Data | Dictionary/object passed to the script via inputs.json | {} |
Default Code Template
# Read inputs from inputs.json
import json
with open('inputs.json') as f:
inputs = json.load(f)
# Your code here
result = {
'message': 'Hello from Python!',
'data': inputs
}
# Write result to output.json (this becomes the node's output)
with open('output.json', 'w') as f:
json.dump(result, f)
# stdout is for logging only (visible in logs, not returned as output)
print('Processing completed successfully')
Outputs
| Output | Type | Description |
|---|---|---|
| Done | Event | Fires when execution completes successfully or with error |
| Result | Data | Contains the JSON data written to output.json, or an error object if execution failed. Error format: { error: { message, stdout, stderr }, buildLogs?: string } |
Error Handling
When execution fails, the result output contains:
error.message: The error message or exception detailserror.stdout: Standard output from the executionerror.stderr: Standard error outputbuildLogs: Build logs if package installation failed (only present on build failures)
Runtime Behavior and Defaults
- Package Installation: Packages are installed via pip before code execution. Specify versions using standard pip syntax (
package==version). If the input is provided as an array via connection, each element is treated as a package name. - Prebuild Phase: Runs before package installation. Use for system-level setup (e.g., installing system libraries required by Python packages).
- Input Serialization: The
inputsdata socket is automatically serialized toinputs.jsonin the execution environment. Complex objects, arrays, and primitives are supported. - Output Capture: The contents of
output.jsonare parsed as JSON and emitted from theresultoutput. Ensure your code writes valid JSON. - Execution Limits:
- Maximum 10 minutes total execution time (includes prebuild, package installation, and code execution)
- Maximum 600 polling attempts with 1-2 second intervals
- Memory and CPU limits apply based on the serverless runtime configuration
Example Usage
Basic Data Processing
Process incoming data and return a transformed result:
import json
# Read inputs
with open('inputs.json') as f:
data = json.load(f)
# Process data (e.g., calculate statistics)
numbers = data.get('numbers', [])
average = sum(numbers) / len(numbers) if numbers else 0
# Write result
with open('output.json', 'w') as f:
json.dump({
'average': average,
'count': len(numbers),
'sum': sum(numbers)
}, f)
Using External Packages
Install and use external libraries like requests or pandas:
Packages input: requests==2.31.0, pandas
import json
import requests
import pandas as pd
with open('inputs.json') as f:
config = json.load(f)
# Fetch data
response = requests.get(config['url'])
data = response.json()
# Process with pandas
df = pd.DataFrame(data['results'])
summary = df.describe().to_dict()
with open('output.json', 'w') as f:
json.dump(summary, f)
With Prebuild Script
Install system dependencies before Python packages:
Prebuild Script:
apt-get update && apt-get install -y libxml2-dev libxslt1-dev
Packages: lxml, beautifulsoup4
from bs4 import BeautifulSoup
import json
with open('inputs.json') as f:
html_content = json.load(f)['html']
soup = BeautifulSoup(html_content, 'lxml')
text = soup.get_text()
with open('output.json', 'w') as f:
json.dump({'clean_text': text}, f)