[SOLVED] Python in self-hosted n8n: 3 cascading errors and their exact fixes (Docker, VPS)

Environment

  • n8n v2.23.4

  • Self-hosted on Hostinger VPS (Ubuntu 24)

  • Docker Compose deployment

  • Official n8nio/n8n image

Context

I wanted to use the Python Code node in n8n to process data with pandas. What looked like a 10-minute setup turned into 3 hours of cascading debugging. Here are the 3 errors in the exact order they appear, and how to fix each one.

ERROR 1 — Python code execution is not enabled

Exact message:

Python code execution is not enabled

Cause: n8n doesn’t enable Python runners by default. You need two environment variables in your docker-compose.yml:

environment:
  - N8N_RUNNERS_ENABLED=true
  - N8N_RUNNERS_MODE=external

Restart the container. This error goes away — and you immediately hit the next one.

ERROR 2 — Security violations detected (all imports blocked)

Exact message:

Security violations detected
Line 1: Import of standard library module 'sys' is disallowed. Allowed stdlib modules: none
Line 2: Import of external package 'pandas' is disallowed. Allowed external packages: none

Cause: This is the sneaky one. The official n8nio/n8n image ships with a hardcoded /etc/n8n-task-runners.json that silently overrides N8N_RUNNERS_STDLIB_ALLOW and N8N_RUNNERS_EXTERNAL_ALLOW — even if you set them in your docker-compose.yml. Result: zero imports allowed, not even sys.

Fix: build a custom Docker image that corrects this file and pre-installs your packages.

Create a Dockerfile:

FROM n8nio/n8n:latest

USER root

# Fix the Python runner config file
RUN echo '{ \
  "runners": [{ \
    "runnerType": "python", \
    "maxConcurrency": 5, \
    "python": { \
      "allowedModules": "*", \
      "allowedExternalModules": "*" \
    } \
  }] \
}' > /etc/n8n-task-runners.json

# Install required Python packages into the runner venv
RUN cd /opt/runners/task-runner-python && \
    .venv/bin/pip install pandas numpy requests

USER node

In your docker-compose.yml, replace the image with your local build:

services:
  n8n:
    build: .
    # image: n8nio/n8n   ← comment this out

Rebuild and restart. Imports work — and you hit the next error.

ERROR 3 — No module named ‘pandas’

Exact message:

No module named 'pandas'

Cause: Even if pandas is installed somewhere in the container, the Python runner runs inside its own isolated virtualenv: /opt/runners/task-runner-python/.venv/. Packages installed outside this venv are invisible to it.

Fix: the pip install step in the Dockerfile above handles this — but you must target the runner’s venv explicitly (cd /opt/runners/task-runner-python && .venv/bin/pip install ...).

If you previously ran a plain pip install pandas inside the container without going through that venv, that’s why it didn’t work.

Final result

With this custom image, this runs cleanly in the Python Code node:

import pandas as pd
import sys

df = pd.DataFrame({"a": [1, 2, 3]})
print(df)
print(sys.version)

The takeaway

The official n8n image is not ready out of the box for Python in self-hosted setups. The /etc/n8n-task-runners.json file takes priority over your environment variables — and this is not clearly documented anywhere. A custom image is the only clean fix.

Hope this saves someone else 3 hours.

2 Likes

This is actually not needed

Alternate search terms that lead to this issue:

  • n8n python imports not working

  • n8n python code node disabled

  • n8n security violation import disallowed

  • n8n allowed stdlib modules none

  • n8n allowed external packages none

  • n8n pandas not found self-hosted

  • n8n task runner python venv

  • n8n-task-runners.json override env variable

  • N8N_RUNNERS_STDLIB_ALLOW not working

  • N8N_RUNNERS_EXTERNAL_ALLOW ignored

  • n8n python no module named pandas docker

  • n8n custom docker image python packages

  • n8n external runner python blocked

  • n8n v2 python code execution not enabled

If any of these brought you here, the full fix is in the post above.

Why? Could you explain?

In n8n v2.0 and later, task runners are permanently enabled by default to isolate and safely execute code inside Code nodes. The N8N_RUNNERS_ENABLED environment variable has been officially deprecated and removed.

2 Likes

The /etc/n8n-task-runners.json override being hardcoded in the official image is definitely the least obvious part - easy to miss if you’re not inspecting the container. One more option worth knowing: instead of rebuilding a custom image, you can bind-mount your own config file directly via volumes in docker-compose.yml (- ./n8n-task-runners.json:/etc/n8n-task-runners.json), which makes it easier to update without a rebuild.

2 Likes

I appreciate. Thank you

2 Likes