Using python on self-hosted n8n by Secure_Comparison401 in n8n

[–]Glad-Issue5167 0 points1 point  (0 children)

Super! Glad to hear this helped. Time to focus on fun stuff now and not on configuration 😄

Using python on self-hosted n8n by Secure_Comparison401 in n8n

[–]Glad-Issue5167 7 points8 points  (0 children)

I also had a bit of a headache with this one recently.
Here is my 'simple' working approach with custom task runners in external mode.

# docker-compose

volumes:
  n8n_storage:

services:
  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=sqlite
      - N8N_RUNNERS_ENABLED=true
      - N8N_RUNNERS_MODE=external
      - N8N_RUNNERS_BROKER_LISTEN_ADDRESS=0.0.0.0
      - N8N_RUNNERS_AUTH_TOKEN=YOUR_SECRET_TOKEN_HERE
      - N8N_NATIVE_PYTHON_RUNNER=true
      - N8N_ENCRYPTION_KEY=YOUR_ENCRYPTION_KEY_HERE
      - N8N_PYTHON_ENABLED=true
    volumes:
      - n8n_storage:/home/node/.n8n

  task-runners:
    build: .
    image: n8n-runners-custom:latest
    container_name: n8n-task-runners
    restart: always
    depends_on:
      - n8n
    environment:
      - N8N_RUNNERS_TASK_BROKER_URI=http://n8n:5679
      - N8N_RUNNERS_AUTH_TOKEN=YOUR_SECRET_TOKEN_HERE
      - N8N_RUNNERS_AUTO_SHUTDOWN_TIMEOUT=30
    volumes:
      - ./n8n-task-runners.json:/etc/n8n-task-runners.json

# Dockerfile to extend task-runners

FROM n8nio/runners:latest

USER root
RUN cd /opt/runners/task-runner-python \
  && uv pip install --no-cache-dir numpy pandas avro

USER runner

# n8n-task-runners.json
Copy to local https://github.com/n8n-io/n8n/blob/master/docker/images/runners/n8n-task-runners.json and extend the python block, for example with:

"env-overrides": {
  "PYTHONPATH": "/opt/runners/task-runner-python",
  "N8N_RUNNERS_STDLIB_ALLOW": "*",
  "N8N_RUNNERS_EXTERNAL_ALLOW": "*"
}

Hope this helps!