n8n/packages/nodes-base/nodes/Code/descriptions/PythonCodeDescription.ts
Mutasem Aldmour 29d29ac753
fix(core): Stop the workflow builder writing Python Code nodes that cannot run
The native Python runner allows no imports unless the deployment sets
N8N_RUNNERS_STDLIB_ALLOW, which is empty by default and forced empty in the
official runners image. It also defines only `_items`, `_item` and `print`.
The builder had no way to know either constraint: the `pythonCode` builderHint
covered only network access, and the property description still advertised
`_today` and `_jmespath`, which went away with Pyodide.

- Correct the `pythonCode` description and builderHint, and add a `language`
  builderHint that steers to JavaScript unless Python was asked for.
- Add `CODE_NODE_PYTHON_IMPORT` and `CODE_NODE_PYTHON_UNSUPPORTED_GLOBAL` to
  the Code node Python linter, so `workflow-sdk validate` reports both defects.
- Note the constraints in the workflow-builder skill.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-26 10:09:44 +02:00

55 lines
2.3 KiB
TypeScript

import type { INodeProperties } from 'n8n-workflow';
const commonDescription: INodeProperties = {
displayName: 'Python',
name: 'pythonCode',
type: 'string',
typeOptions: {
editor: 'codeNodeEditor',
editorLanguage: 'python',
},
default: '',
description:
'Python code to execute. Runs in a sandbox with no imports and no network access: read the input items from <code>_items</code> (all-items mode) or <code>_item</code> (per-item mode). <a href="https://docs.n8n.io/code/builtin/">Learn more</a>.',
noDataExpression: true,
builderHint: {
propertyHint:
'Runs in a locked-down native Python sandbox. NO imports: the stdlib allowlist (N8N_RUNNERS_STDLIB_ALLOW) is empty by default, so `import re`, `import json`, `import math`, `import datetime` and every third-party package FAIL at runtime with "Import of standard library module \'…\' is disallowed". Write import-free Python using builtins and str/list/dict methods, or set language to javaScript when the task genuinely needs a library. NO network access either: requests, urllib, httpx and other HTTP libraries are unavailable — use the HTTP Request node and process its output in this node instead. The ONLY globals are _items (runOnceForAllItems mode), _item (runOnceForEachItem mode) and print(); there are no cross-node helpers — _("Node Name"), _input, _json, _today and _jmespath are all undefined and raise NameError, so read data from the connected upstream node only.',
},
};
const PRINT_INSTRUCTION =
'Debug by using <code>print()</code> statements and viewing their output in the browser console.';
export const pythonCodeDescription: INodeProperties[] = [
{
...commonDescription,
displayOptions: {
show: {
language: ['python', 'pythonNative'],
mode: ['runOnceForAllItems'],
},
},
},
{
...commonDescription,
displayOptions: {
show: {
language: ['python', 'pythonNative'],
mode: ['runOnceForEachItem'],
},
},
},
{
displayName: `${PRINT_INSTRUCTION}<br><br>The Python option does not support <code>_</code> syntax and helpers, except for <code>_items</code> in all-items mode and <code>_item</code> in per-item mode.<br><br>Imports are disabled unless your instance sets <code>N8N_RUNNERS_STDLIB_ALLOW</code>.`,
name: 'notice',
type: 'notice',
displayOptions: {
show: {
language: ['python', 'pythonNative'],
},
},
default: '',
},
];