data/skills/n8n-code-python/README.md
Expert guidance for writing Python code in n8n Code nodes.
Use JavaScript for 95% of use cases.
Python in n8n has NO external libraries (no requests, pandas, numpy).
When to use Python:
When to use JavaScript (recommended):
_input.all(), _input.first(), _input.item_json["body"][{"json": {...}}]This skill emphasizes error prevention:
These 5 errors are the most common in Python Code nodes.
This skill activates when you:
Example queries:
Quick start and overview
Complete data access patterns
_input.all() - Process all items_input.first() - Get first item_input.item - Current item (Each Item mode)_node["Name"] - Reference other nodesAvailable Python modules
10 production-tested patterns
Top 5 errors with solutions
This skill works with:
get_nodeAfter using this skill, you should be able to:
_input.all(), _input.first(), _input.item_json["body"][{"json": {...}}].get() for dictionary accessall_items = _input.all()
first_item = _input.first()
current_item = _input.item # Each Item mode only
other_node = _node["NodeName"]
webhook = _input.first()["json"]
body = webhook.get("body", {})
name = body.get("name")
# ✅ Use .get() with defaults
value = data.get("field", "default")
# ❌ Risky - may raise KeyError
value = data["field"]
# ✅ Correct format
return [{"json": {"result": "success"}}]
# ❌ Wrong - plain dict
return {"result": "success"}
# ✅ Available
import json
import datetime
import re
import base64
import hashlib
# ❌ NOT available
import requests # ModuleNotFoundError!
import pandas # ModuleNotFoundError!
import numpy # ModuleNotFoundError!
webhook = _input.first()["json"]
body = webhook.get("body", {})
return [{
"json": {
"name": body.get("name"),
"email": body.get("email"),
"processed": True
}
}]
all_items = _input.all()
active = [
{"json": {**item["json"], "filtered": True}}
for item in all_items
if item["json"].get("status") == "active"
]
return active
import statistics
all_items = _input.all()
amounts = [item["json"].get("amount", 0) for item in all_items]
return [{
"json": {
"total": sum(amounts),
"average": statistics.mean(amounts) if amounts else 0,
"count": len(amounts)
}
}]
import json
data = _input.first()["json"]["body"]
json_string = data.get("payload", "{}")
try:
parsed = json.loads(json_string)
return [{"json": parsed}]
except json.JSONDecodeError:
return [{"json": {"error": "Invalid JSON"}}]
Problem: No requests library
Workaround: Use HTTP Request node or JavaScript
Problem: No pandas or numpy
Workaround: Use list comprehensions and standard library
Problem: No psycopg2, pymongo, etc.
Workaround: Use n8n database nodes (Postgres, MySQL, MongoDB)
Problem: No beautifulsoup4 or selenium
Workaround: Use HTML Extract node
[{"json": {...}}]if items:)Use Python when:
Use JavaScript instead when:
Beginner:
_input patterns.get()Intermediate: 4. Study STANDARD_LIBRARY.md - Know what's available 5. Try COMMON_PATTERNS.md examples - Use proven patterns 6. Learn ERROR_PATTERNS.md - Avoid common mistakes
Advanced: 7. Combine multiple patterns 8. Use standard library effectively 9. Know when to switch to JavaScript 10. Write production-ready code
Questions?
Related Skills:
Version: 1.0.0 Status: Production Ready Compatibility: n8n Code node (Python mode)
Part of the n8n-skills project.
Conceived by Romuald Członkowski
Remember: JavaScript is recommended for 95% of use cases. Use Python only when you specifically need Python's standard library features.