n8n-code-python
Skill này làm gì
Skill này giúp bạn viết code Python bên trong node Code của n8n cho n8n 2.x. Nó dành cho người muốn dùng Python thay vì JavaScript và đang sửa code chép từ các template cũ.
Khi nào nên dùng
- ✓Khi bạn đặc biệt muốn dùng Python trong một node Code của n8n.
- ✓Khi code Python cũ từ template n8n bị lỗi NameError sau khi chuyển sang n8n 2.x.
- ✓Khi một node Code Python báo vi phạm bảo mật, import bị chặn hoặc thiếu hàm có sẵn.
Liên kết skill
https://github.com/czlonkowski/n8n-skills/blob/main/skills/n8n-code-python/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Node Code Python của n8n tôi báo NameError ở _input, làm sao để cập nhật nó cho phiên bản mới?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill giải thích rằng Python trong n8n 2.x chạy gốc với ít hàm trợ giúp có sẵn hơn, chỉ các biến mới cần dùng, và lưu ý rằng import thư viện chuẩn bị chặn mặc định.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Python Code Node (native)
Since n8n 2.0 the Code node's Python runs as native Python in a task runner (language: "pythonNative"). The old Pyodide "Python (Beta)" is gone, and with it every n8n helper it had:
_input, _json, _node, _now, _today and _jmespath all raise NameError now. Code copied
from old templates, forum posts or older docs usually breaks on this.
JavaScript first — stronger than before
Native Python gives you two variables and plain Python, and by default no imports: no
json, datetime or re. Everything n8n-specific ($('Node'), $jmespath, Luxon,
this.helpers.httpRequest, static data) exists only in JavaScript. Use Python only when the user
explicitly asks for it. Even then, first check whether an expression, Edit Fields, or a native
node (Crypto, Date & Time, HTML, XML) does the job. See n8n-code-javascript and the transform
gatekeeper in n8n-expression-syntax.
Quick start
# Run Once for All Items (default mode)
return [
{"json": {"name": it["json"]["name"], "revenue": it["json"]["revenue"]}}
for it in _items
if it["json"].get("active")
]
# Run Once for Each Item
row = _item["json"]
return {"json": {**row, "name_upper": (row.get("name") or "").upper()}}
Node config: {"language": "pythonNative", "mode": "runOnceForAllItems" | "runOnceForEachItem", "pythonCode": "..."} on n8n-nodes-base.code typeVersion 2.
The only inputs: _items and _item
| Mode | Variable | Shape |
|---|---|---|
| Run Once for All Items | _items |
list of plain dicts {"json": {...}, "pairedItem": {...}} |
| Run Once for Each Item | _item |
one plain dict {"json": {...}, "pairedItem": {...}} |
- Each variable exists only in its own mode.
_itemsin each-item mode (or_itemin all-items mode) raisesNameError. - Dict access only.
it["json"]["name"]orit["json"].get("name").it.json.nameraisesAttributeError: 'dict' object has no attribute 'json'. - No other nodes. There is no
_node/$('Node')equivalent. If you need data from another branch, bring it in with Merge first, or read it in JavaScript. - Webhook payloads are under
["body"]:_items[0]["json"].get("body", {}).get("email"). - Binary data isn't covered here. Read and write binary in JavaScript (see n8n-binary-and-data).
- pairedItem: returning
_items/_itemkeeps it. When you build new dicts and downstream uses$('Node').item, add"pairedItem": {"item": i}to each returned item. That isn't verified on native Python yet, so test-run before relying on it. - Missing keys: prefer
.get(key, default).row["missing"]raisesKeyError.
Migration table for legacy code:
| Legacy (Pyodide) | Native |
|---|---|
_input.all() |
_items |
_input.first()["json"] |
_items[0]["json"] (guard if _items) |
_input.item / _json |
_item / _item["json"] |
_node["X"] |
not available: Merge upstream, or use JS |
_now, _today |
not available: pass {{ $now.toISO() }} in via Edit Fields, or use JS |
_jmespath(data, q) |
not available: $jmespath in an expression, or a comprehension |
item.json.field |
item["json"]["field"] |
Imports: blocked by default
Every import (standard library and third-party) is checked against an allowlist before the
code runs. The default allowlist is empty, so even import json rejects the whole node:
Security violations detected
Line 1: Import of standard library module 'json' is disallowed. Allowed stdlib modules: none
- n8n Cloud: no imports at all.
- Self-hosted: the admin can allowlist modules in the task-runner config (see
n8n-self-hosting →
TASK_RUNNERS.md). Some instances therefore allowjson,datetimeandre, and most don't. - Default to import-free code. If an import would really help, confirm it first: a
one-line test node
import json+return [{"json": {"ok": True}}], or ask the user. Never assume. - Without imports: parse JSON strings upstream (
{{ JSON.parse($json.payload) }}in Edit Fields). Do date math with the Date & Time node or Luxon in an expression. ISO-8601 strings still compare and sort correctly as plain strings. Use the Crypto node for hashing. requests,pandasandnumpyare never available unless the admin built a custom runner image. Use the HTTP Request node for HTTP.
Sandbox limits (these fail even without import
Câu hỏi thường gặp
Vì sao code Python cũ của n8n bị lỗi?
Các biến trợ giúp cũ như _input không còn tồn tại trong n8n 2.x, nên code phải được cập nhật.
Tôi có import được thư viện Python không?
Import thư viện chuẩn bị tắt mặc định, nên skill giải thích cách điều chỉnh code.
Tôi có nên chọn Python thay vì JavaScript không?
Skill nói chỉ dùng Python khi bạn thật sự muốn, vì JavaScript có nhiều tính năng n8n hơn.
Chi tiết
Mô tả gốc của tác giả
Write Python in n8n Code nodes (native Python, `language` pythonNative, n8n 2.x). Use when the user explicitly wants Python in a Code node, when migrating old Pyodide/"Python (Beta)" code that used _input/_json/_node/_now, or when a Python Code node fails with NameError, "Security violations detected", "Import of standard library module … is disallowed", "__build_class__ not found", "A 'json' property isn't a dictionary", or "Python runner unavailable". Covers the only two variables (_items/_item), dict-only access, imports blocked by default, the sandbox's denied builtins, accepted return sha
repo: czlonkowski/n8n-skills
Độ phổ biến
- Sao GitHub
- 6.6k
- Vị trí xếp hạng
- #13869
- Giấy phép
- MIT
- Cập nhật
- 24/10/2026
Bình luận
- …
Thêm skill từ kho mã này
Skill tương tự
knowledge-automation
394kGiúp agent của bạn thiết lập tác vụ định kỳ đưa nội dung mới vào kho Knowledge theo lịch, rồi kiểm tra tác vụ có chạy không.
knowledge-source-ingest
394kGiúp agent của bạn cập nhật Knowledge vault với các mục mới từ kênh chat, nguồn tin hoặc hộp thư, mà không thêm lại những gì đã có.
benchmark
280kGiúp agent của bạn đo tốc độ và hiệu suất của code, hữu ích để đảm bảo mọi thứ nhanh và tìm ra những chỗ chậm.