Overview
This node allows users to run custom Python scripts within an n8n workflow, providing a flexible environment to execute arbitrary Python code. It supports injecting input data and environment variables (including credentials) into the script, processing binary files from previous nodes, and handling output files generated by the script.
Common scenarios where this node is beneficial include:
- Running complex data transformations or calculations that are easier in Python.
- Integrating with external APIs or services using Python libraries.
- Processing files or images using Python scripts.
- Automating tasks that require Python scripting capabilities within an n8n workflow.
Practical example:
- A user wants to fetch data from an API, process it with a Python script that uses specific credentials, and then output the processed results back into the workflow.
- Another use case is reading binary files passed from previous nodes, analyzing them in Python, and generating new output files for further workflow steps.
Properties
| Name | Meaning |
|---|---|
| Credentials Management | Configure how credentials are included in the Python script. Options include using the default credential, specifying multiple credentials via names list, selectors, dynamic collection, or JSON configuration. Also controls variable merge strategy and hiding variable values. |
| Python Code | The Python script to execute. Can access injected variables like input_items and environment variables if enabled. |
| Code Template Mode | Enable viewing of auto-generated Python code structure around the user script. |
| Extract Code Template | Button to generate the current auto-generated Python code template based on node settings. |
| Auto-Generated Code Template | Displays the auto-generated Python code template with current node settings. |
| Inject Variables | Whether to inject input_items and env_vars variables into the Python script. Enables integration features; disable for pure Python scripts. |
| Python Executable | Path to the Python executable to use (e.g., python3, python, or full path). |
| Error Handling | How to handle Python script errors and non-zero exit codes: return error details, throw error to stop execution, or ignore exit code except system errors. |
| Debug/Test Mode | Debugging options including off, basic debug info, full debug info, test-only mode (validate without executing), and export script with debug info as binary attachment. |
| Script Export Format | File format for exported script in debug mode (.py or .txt). |
| Script Generation Options | Options controlling what is included in the generated script: include input_items array, legacy env_vars dictionary, and select system environment variables to include. |
| File Processing | Configure processing of binary files from previous nodes: enable/disable, file access method (temporary files, base64, or both), max file size, include metadata, and auto-cleanup of temporary files. |
| Output File Processing | Configure automatic detection and processing of files created by the Python script: enable/disable, max output file size, auto-cleanup, include metadata, auto-intercept file writes, expected output filename, and detection mode. |
| Parse Output | How to parse the stdout output from the Python script: raw string, JSON, lines array, or smart auto-detect (JSON, CSV, or lines). |
| Parse Options | Additional options for parsing JSON output: handle multiple JSON objects, strip non-JSON text, fallback on parse error. |
| Execution Mode | Execute script once for all input items (faster) or once per item (more flexible but slower). |
| Pass Through Input Data | Include original input data in the output alongside Python script results. |
| Pass Through Mode | How to include input data when pass-through is enabled: merge with result, separate field, or multiple outputs. |
| File Debug Options | Advanced debugging options for file processing issues: enable detailed file debug info, debug input/output files, include system info, include directory listings. |
Output
The node outputs an array of items where each item contains:
json: An object representing the parsed or raw output from the Python script execution. This includes:exitCode: The exit code of the Python script.stdout: Raw standard output from the script.stderr: Standard error output.success: Boolean indicating if the script exited successfully (exit code 0).error: Error message if any.inputItemsCount: Number of input items processed.executedAt: Timestamp of execution.injectVariables: Whether variables were injected.parseOutput: The output parsing mode used.executionMode: Execution mode used (once or per item).- Parsed output fields such as
parsed_stdout,parsing_success,parsing_error,output_format, andparsing_methoddepending on parsing options. - If output file processing is enabled, includes
outputFilesarray with metadata and base64 content of generated files. - If file debug options are enabled, includes detailed file debug information.
- Detailed error information if execution failed.
binary(optional): Contains binary attachments such as:- Exported Python script files (in debug export mode).
- Output files generated by the Python script (if output file processing is enabled).
- Debug information files.
If pass-through input data is enabled, the output can also include original input data merged or separated according to the selected pass-through mode.
Dependencies
- Requires a Python interpreter installed and accessible at the specified path (default
python3). - Optionally requires credentials configured as environment variables to be injected into the script.
- Supports processing of binary files from previous nodes, requiring temporary file storage permissions.
- For output file processing, requires write/read access to temporary directories.
- No external npm packages are required beyond Node.js built-in modules (
child_process,fs,path).
Troubleshooting
Common Issues
- Python executable not found or incorrect path: Ensure the Python path is correct and Python is installed on the system.
- Syntax errors in Python script: The node validates syntax before execution and reports errors with line numbers.
- Missing Python modules: Errors about missing modules will appear if the script imports unavailable packages. Install required packages in the Python environment.
- File size limits exceeded: Input or output files larger than configured max sizes will cause errors or be skipped.
- Permission errors: Insufficient permissions to create temporary files or output directories will cause failures.
- Credential loading errors: Misconfigured or missing credentials may lead to empty or missing environment variables in the script.
Error Messages and Resolutions
- "Process exited with code X": Indicates the Python script returned a non-zero exit code. Check script logic and error output.
- "Failed to generate temporary script file": Check filesystem permissions and available disk space.
- "File processing failed": Verify input files are correctly formatted and within size limits.
- "Failed to cleanup script" or "Failed to cleanup output directory": Usually warnings; check file locks or permissions.
- "Invalid variable assignment detected": Input data contains invalid keys; ensure input JSON keys are valid Python variable names.
Links and References
- Python official website
- n8n Documentation - Python Function Node
- Python AST module documentation
- Base64 encoding in Python
- Node.js child_process module
This summary describes the static analysis of the provided source code and property definitions for the "Python Function (Raw)" node in n8n, focusing on the Default resource and operation.