n8n doesn't support Python natively. That's the first thing to understand. The Code node runs JavaScript, and while there's a Pyodide option for Python-like syntax, it's severely limited. But you have three real options for getting Python into your workflows, each with different tradeoffs.
The Three Approaches
| Approach | Best For | Limitations |
|---|---|---|
| Code node (Pyodide) | Quick data transformations | No system libraries, no numpy/pandas |
| Execute Command node | Host scripts with dependencies | Requires Docker setup, permission issues |
| External HTTP microservice | Complex, isolated logic | More infrastructure to manage |
Code Node with Pyodide
The Code node offers a Python mode, but it runs through Pyodide - a WebAssembly port of Python. This means JavaScript interprets your Python, which creates hard limits.
What works: Basic string manipulation, simple math, JSON transformations. Anything that doesn't need external packages.
What doesn't work: numpy, pandas, requests, or anything that touches the file system. No pip install. No virtual environments.
Use this when you need a quick script that could honestly be written in JavaScript anyway, but you're more comfortable with Python syntax.
The Pyodide limitations aren't well-documented in n8n's core docs, which is why the community forum is full of confused posts about import errors.
Execute Command Node with Docker
This is where most serious Python work happens in n8n. The Execute Command node runs shell commands on the host machine, so if you can run a Python script from your terminal, you can run it from n8n.
The catch: standard n8n Docker images don't include Python. You need to either install it at runtime or build a custom image.
Quick install approach:
apk add python3 py3-pip
This works but runs every time your container restarts. For production, build a custom Dockerfile.
Custom image approach:
The naskio/n8n-nodes-python project provides pre-built images with Python and common packages. Their n8n-python:latest-debian image includes numpy and pandas out of the box.
As the project maintainer explains: "PythonFunction node is used to run custom Python snippets to transform data or to implement some custom functionality that n8n does not support yet."
Common Gotchas
Permission issues: The n8n user in Docker often can't access Python installations. Run which python3 to verify paths.
python vs python3: Some systems only have python3 in PATH. Always use python3 explicitly.
Virtual environment activation: If using venvs, activate them in the same command:
source /path/to/venv/bin/activate && python3 script.py
Module paths: Globally installed pip packages might not be visible to the n8n user. Install with --user flag or into the venv.
A community expert summarized it well: "To run a Python script with n8n using a virtual environment, you can use the Execute Command node. It basically runs shell commands on the host so if you can run the python script from the shell you can also do it from n8n." (Source)
External Python Microservice
For complex Python applications - ML models, heavy data processing, anything with lots of dependencies - run Python separately and call it via HTTP.
Set up a Flask or FastAPI endpoint that accepts JSON, does the work, returns JSON. The n8n HTTP Request node calls this service.
Why this works better for complex cases:
-
Dependencies managed in their own container
-
No permission conflicts with n8n
-
Scale Python workers independently
-
Easier debugging - test the service outside n8n
The overhead: You're now managing two services instead of one. Worth it when your Python code is substantial, overkill for simple scripts.
Decision Framework
Use Pyodide/Code node when:
-
Script is under 50 lines
-
No external packages needed
-
Data transformation only
Use Execute Command when:
-
You need pip packages
-
Running existing scripts
-
One-off automation tasks
-
You control the host environment
Use external microservice when:
-
ML models or heavy computation
-
Complex dependency trees
-
Multiple workflows share the same Python logic
-
You need to scale processing independently
Setting Up Execute Command - Step by Step
-
Verify Python exists on host: Add an Execute Command node with
python3 --version. If it fails, Python isn't installed. -
Install Python (Alpine-based n8n image):
apk add python3 py3-pip -
Install packages:
pip3 install requests pandas -
Run your script:
python3 /path/to/script.py -
Handle output: The node captures stdout. Print JSON from your Python script, then parse it in the next n8n node.
For scheduled execution, like running a script every hour, the Cron trigger node combined with Execute Command handles this cleanly. (Source)
Building a Custom Docker Image
For production deployments where you need specific Python packages consistently available:
FROM n8nio/n8n:latest
USER root
RUN apk add --no-cache python3 py3-pip
RUN pip3 install pandas requests beautifulsoup4
USER node
Build and push to your registry, then reference in your deployment.
Alternatively, use existing community images. The naskio/n8n-nodes-python repo maintains several variants including Debian-based images for packages that don't compile on Alpine.
Passing Data Between n8n and Python
The cleanest pattern: JSON in, JSON out.
In your n8n workflow:
- Use a Set node to prepare data as JSON
- Execute Command runs:
echo '{{ JSON.stringify($json) }}' | python3 script.py - Python reads from stdin, processes, prints JSON to stdout
- Next node parses the output
In your Python script:
import sys
import json
data = json.load(sys.stdin)
result = process(data)
print(json.dumps(result))
This avoids file system complexity and keeps everything in memory.
FAQ
Does n8n support Python natively?
No. The Code node runs JavaScript. There's a Pyodide option that interprets Python-like syntax through WebAssembly, but it can't use standard Python packages like numpy or pandas.
Why can't I import pandas in the Code node?
The Code node's Python mode uses Pyodide, which runs in the browser/JavaScript environment. System-level packages and compiled libraries aren't available. Use the Execute Command node with a proper Python installation instead.
How do I install pip packages in n8n?
Use the Execute Command node to run pip3 install package-name. For persistent installs, either build a custom Docker image or add installation commands to your container startup. (Source)
Can I use virtual environments with n8n?
Yes, via Execute Command. Activate the venv and run Python in the same command: source /venv/bin/activate && python3 script.py. The venv must be accessible to the n8n user.
What's the best approach for running ML models?
External microservice. Package your model in a Flask/FastAPI app, run it as a separate container, and call it via n8n's HTTP Request node. This keeps dependencies isolated and lets you scale inference separately from your workflow engine.
Why does my Python script work locally but fail in n8n?
Usually permissions or paths. The n8n container runs as a specific user who may not have access to system Python or installed packages. Verify with whoami and which python3 in Execute Command nodes.
Need help building Python-powered n8n workflows? n8nlogic.com specializes in automation services that handle the infrastructure complexity so you can focus on your business logic.