Python in the n8n Code node
Updated 2026-10-21 · 5 min read
- In n8n 2.x Python runs natively; read input with _items or _item, fields with brackets.
- On n8n Cloud, Python can't import any library, not even datetime.
- Self-hosted Python needs the n8nio/runners sidecar in external mode.
The n8n Code node runs Python as well as JavaScript. In n8n 2.x it runs native Python through task runners. A task runner is the separate process n8n uses to run Code node code. n8n 1.x used the Pyodide engine instead, which is CPython compiled to WebAssembly. Native Python is real CPython, but it has a much smaller API surface inside n8n. That difference trips up almost every script copied from older tutorials. This page covers what works, how to set it up on self-hosted n8n, and examples written for the current runtime.
Native Python vs the old Pyodide version
This comparison is based on the Code node docs and the 2.0 breaking changes:
| Pyodide (legacy, removed in 2.0) | Native Python (1.111.0+, stable in 2.x) | |
|---|---|---|
| Engine | CPython compiled to WebAssembly | CPython in a task runner |
| Input | _input.all(), _json, _('Node')… |
Only _items (all items) and _item (each item) |
| Field access | item.json.field worked |
Only item["json"]["field"] |
| Packages | The ones bundled with Pyodide | Cloud: none. Self-hosted: allow-listed |
| Static data | No | No (JavaScript only) |
Tutorials that use _input.all() or dot access were written for Pyodide. On 2.x, rewrite them with _items and brackets. n8n's reference-previous-nodes page still shows a Python tab with _input. That tab is legacy content.
The two modes
Run Once for All Items (the default): _items is a list of items. Return a list of dicts.
out = []
for item in _items:
data = item["json"]
out.append({"json": {
"email": data["email"].lower(),
"domain": data["email"].split("@")[1],
}})
return out
Run Once for Each Item: _item is the current item. Return one dict.
data = _item["json"]
data["total_with_vat"] = round(data["total"] * 1.2, 2)
return _item
Every item has a json key that holds a dict. Return items in that shape yourself. The docs promise automatic wrapping only for JavaScript.
Examples
Aggregate
total = sum(float(i["json"].get("amount", 0)) for i in _items)
return [{"json": {"total": total, "count": len(_items)}}]
Filter
return [i for i in _items if i["json"].get("status") == "paid"]
Explode a list field
order = _items[0]["json"]
return [{"json": {"order_id": order["id"], **line}} for line in order["lines"]]
Dates without imports
On n8n Cloud you can't import even datetime. So pass dates in as ISO strings and compare them as strings. Or compute dates in an expression before the Code node, with {{ $now.toISO() }}. ISO-8601 strings in the same timezone sort correctly as text.
cutoff = _items[0]["json"]["cutoff"] # set by an Edit Fields node
return [i for i in _items if i["json"]["created_at"] >= cutoff]
What isn't available
- Other node data. Native Python has no
$('Node')equivalent. First pass what you need into the Code node's input, with an Edit Fields or Merge node. - HTTP requests and files. The Code node can't make HTTP requests or access the file system. Use the HTTP Request node and Read/Write File From Disk.
- Credentials. Code nodes can't read credentials.
- Static data.
getWorkflowStaticDataexists only in the JavaScript Code node (docs). Use Data tables for state. - Unsafe built-ins. By default
N8N_RUNNERS_BUILTINS_DENYblockseval,exec,compile,open,input,getattr,setattr,type,globalsand others (task runner variables).
If a script needs any of these, the JavaScript Code node usually gets you further. It has $('Node'), $vars and static data.
Imports
| Where | Standard library | Third-party (pandas, numpy, requests) |
|---|---|---|
| n8n Cloud | Not allowed | Not allowed |
| Self-hosted, default runners image | Only what you allowlist in N8N_RUNNERS_STDLIB_ALLOW |
Only what's installed and listed in N8N_RUNNERS_EXTERNAL_ALLOW |
N8N_RUNNERS_ALLOW_TRANSITIVE_IMPORTS defaults to false. So if a package imports another package, you may need to allow-list both.
Self-hosted setup
Python Code nodes need task runners in external mode. The runner lives in a separate n8nio/runners container. It holds the launcher, the JavaScript runner and the Python runner. Its version must match n8nio/n8n (task runners). Since 2.0, the main image no longer contains the external-mode runner.
To add packages, extend the runners image. You need version 1.121.0 or later.
FROM n8nio/runners:<same version as n8nio/n8n>
USER root
RUN cd /opt/runners/task-runner-python && uv pip install numpy pandas
COPY n8n-task-runners.json /etc/n8n-task-runners.json
USER runner
Then allow-list the packages in n8n-task-runners.json:
{
"task-runners": [
{
"runner-type": "python",
"env-overrides": {
"PYTHONPATH": "/opt/runners/task-runner-python",
"N8N_RUNNERS_STDLIB_ALLOW": "json,datetime,re",
"N8N_RUNNERS_EXTERNAL_ALLOW": "numpy,pandas"
}
}
]
}
The full Compose file with the runners sidecar is in n8n Docker setup.
Debugging
While you test in the editor, print() output goes to the browser console, like console.log() in JavaScript. Pin the input data on the node. Then you can rerun it without calling upstream APIs.
Common errors:
| Error | Cause |
|---|---|
NameError: _input |
Code from the Pyodide era. Use _items / _item |
AttributeError: 'dict' object has no attribute 'json' |
Dot access. Use brackets |
| Import blocked | The module is not allow-listed (or you're on Cloud) |
| Python option fails on self-hosted | No external-mode runners sidecar |
Python or JavaScript?
Pick Python when the logic is pure data manipulation and your team reads Python more easily. Pick JavaScript when you need other nodes' data, $vars, static data, or modules on n8n Cloud (crypto, moment). Many teams keep Python only for self-hosted instances with pandas installed.
From Pipedream Python steps
Pipedream ran Python 3.12 with def handler(pd). It read data from pd.steps and installed any package automatically on import (Pipedream Python). That model doesn't carry over. pd.steps becomes the node input, imports are restricted, and HTTP calls move to HTTP Request nodes. The migration mapping is in Pipedream Python steps to n8n.
Our converter wraps Python steps in a Python Code node with a compatibility layer. It flags each import, so you see which steps need self-hosted runners and which need a rewrite. Test every converted Python step on your runner. The default deny list of built-ins is strict. A step that fails there needs a rewrite in plain functions. Check one step with the Code node compatibility check, or a whole export with the converter. The report and 1 workflow are free. Full export: $3 per workflow, $1 each after 50, $19 minimum, one-off.
Not affiliated with Pipedream or n8n.
FAQ
Can n8n run Python code?
Yes. The Code node runs Python natively through task runners, available from n8n 1.111.0 and stable in n8n 2. The older Pyodide-based Python was removed in 2.0.
How do I read input items in the n8n Python Code node?
Use _items in Run Once for All Items mode and _item in Run Once for Each Item mode. Native Python doesn't support other built-ins such as _input or _json.
Can I import pandas or requests in n8n Python?
Not on n8n Cloud. There the Python Code node can't import any library, standard or third-party. On self-hosted n8n, add packages to the n8nio/runners image and allowlist them.
Why do I get an error with item.json.field in Python?
Native Python only accepts bracket access: item["json"]["field"]. Dot access worked only in the legacy Pyodide version.
Does Python work in the default n8n Docker image?
Python Code nodes need task runners in external mode, which means the n8nio/runners sidecar container. Since 2.0 the main n8nio/n8n image doesn't include the external-mode runner.
Sources (6)
- https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.code
- https://docs.n8n.io/deploy/host-n8n/configure-n8n/set-up-task-runners
- https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/task-runners
- https://docs.n8n.io/changelog/v20-breaking-changes
- https://docs.n8n.io/build/code-in-n8n/cookbook/built-in-methods-and-variables-examples/getworkflowstaticdata
- https://pipedream.com/docs/workflows/building-workflows/code/python