Home » n8n» Run Python code in n8n — Beginner Docker Guide

Run Python code in n8n?

You can run Python code in n8n by extending the official Docker image to include Python and then using the Execute Command node to call your scripts. Run Python code in n8n by mounting a scripts folder and executing python3 from the workflow. This approach works well for simple automation tasks and prototypes.

What You Need

  • Docker or Docker Compose installed on your machine.
  • Basic knowledge of n8n and its Execute Command node.
  • Python 3 scripts you want to run, placed in a local scripts folder.
  • A custom n8n Docker image that includes Python.

Run Python code in n8n: Step-by-step

Step 1: Create a scripts folder and a sample Python file

Create the folder and a simple script that prints JSON. This makes it easy to capture output in n8n.

mkdir -p ~/n8n-scripts
cat > ~/n8n-scripts/hello.py <<'PY'
import json
print(json.dumps({"message": "Hello from Python", "status": "ok"}))
PY

Step 2: Build a custom n8n image that includes Python

Extend the official n8n image and install Python. Save this Dockerfile next to your scripts folder or in a build folder.

cat > Dockerfile <<'DF'
FROM n8nio/n8n:latest
USER root
RUN apt-get update && apt-get install -y python3 python3-pip && rm -rf /var/lib/apt/lists/*
USER node
DF

Build the image:

docker build -t n8n-python:latest .

Step 3: Run the container and mount your scripts

Run n8n with the scripts folder mounted to /data/scripts inside the container. Expose port 5678 for the n8n UI.

docker run -it --rm --name n8n-python \
  -p 5678:5678 \
  -v ~/n8n-scripts:/data/scripts:ro \
  -v ~/.n8n:/home/node/.n8n \
  n8n-python:latest

Step 4: Create a workflow that calls your Python script

In the n8n editor, add an Execute Command node. Use the following command to run the script and return stdout.

python3 /data/scripts/hello.py

Set the Execute Command node to return the full command output. The node will capture the JSON printed by the script. You can then add a subsequent node to parse the JSON if needed.

Step 5: Pass data into Python (optional)

To pass workflow data to Python, write data to stdin or to a temp file. Example using echo and piping JSON to python:

echo '{"name":"Alice"}' | python3 /data/scripts/process_input.py

In process_input.py, read stdin and parse JSON. This lets Python act on dynamic workflow data.


Update

When you change Python code, update the scripts in your host folder. If you change system dependencies, update the Dockerfile and rebuild the image:

docker build -t n8n-python:latest .
docker stop n8n-python && docker run ... n8n-python:latest

Keep n8n and Python versions compatible. Rebuild when you need new packages.

Security

  • Do not run untrusted scripts. Validate and limit inputs before execution.
  • Mount script folders read-only when possible (-v host:container:ro).
  • Run the container with the least privileges needed. Avoid exposing sensitive ports to public networks.
  • Limit file system access and use environment variables for secrets, not plaintext files.

Done

You now have a simple, repeatable way to run Python scripts from n8n using Docker. Extend the approach for libraries, virtual environments, or complex tasks. Test workflows carefully before using them in production.

Neil
Written by Neil

Neil is a true n8n geek who lives and breathes workflow automation. He dives deep into nodes, triggers, webhooks, custom logic, and self-hosting setups, sharing everything he learns about n8n on AutomationCompare.com. As part of a broader team of automation specialists, Neil focuses purely on mastering n8n and helping others unlock its full potential.

Keep Reading

Scroll to Top