Running Native Python Code Inside an n8n Instance on a Hostinger VPS

Discover how to easily run Python scripts directly in your self-hosted n8n workflows.
n8n
VPS
Python
Author

David Gwyer

Published

June 4, 2025

Recently I needed to run a Python package via pipx directly in an n8n workflow instance, hosted on a Hostinger VPS. I ran into a couple of issue but got it working in the end, so I thought I’d make a blog post out of it in case it proves useful to someone else trying to do the same thing.

The goal is to try and enable native Python script execution directly within n8n workflows. But I didn’t want to just be able to use core packages inside Python scripts, I also wanted to access functions from libraries such as NumPy and fastcore.

Hosting n8n

I chose to host an n8n instance on a Hostinger VPS as I wanted to create workflows that could be triggered to run any time of day uninterrupted. And hostinger seemed like a cost effective solution. And it’s pretty easy to setup the n8n instance. Once done the dashboard looks like this:

Motivation: Why Run Python in n8n?

n8n is a powerful automation tool primarily built around JavaScript and external API integrations. However, Python’s extensive ecosystem and ease of use for data processing tasks make it highly desirable to run Python scripts directly inside n8n workflows. Native Python execution opens opportunities for data manipulation, scientific computations, and more advanced automation scenarios directly within n8n!

Initial Attempt: Modifying the Docker Container

Initially, I assumed I could simply install and run Python commands directly within the n8n Docker container. However, the official n8n image didn’t include Python or package managers like pip, leading to immediate failure. I tried installing Python at container runtime with apt-get update. This failed because the default n8n container image (Alpine-based) doesn’t include apt-get. I realized the correct approach was to build a custom Docker image that included Python, pip, and pipx.

Extending the n8n Docker Image: The Dockerfile

To enable native Python execution, I created the following Dockerfile:

FROM docker.n8n.io/n8nio/n8n:latest

USER root

RUN apk add --no-cache \
    python3 \
    py3-pip \
    py3-virtualenv && \
    pip install --break-system-packages pipx && \
    pipx ensurepath

ENV PATH="/root/.local/bin:${PATH}"

USER node

This provided:

  • Python runtime
  • pip for package management
  • pipx for isolated package environments

Building and Running the Customized Container

I built and started the container using Docker Compose:

docker compose build
docker compose up -d

Verification inside the container confirmed Python and pipx installation:

docker exec -it <container-id> sh
pipx --version
python3 --version

Managing Python Packages (NumPy, Fastcore, etc.)

I encountered a Python system-packaging restriction (PEP 668) when installing packages directly with pip. The solution was using the flag --break-system-packages:

pip install --break-system-packages numpy
pip install --break-system-packages fastcore

This approach bypassed the externally managed environment error safely within our controlled container.

Executing Python Snippets in n8n Workflows

I utilized n8n’s Execute Command node paired with a Set node to store Python scripts and input data.

Set Node Configuration (JSON mode)

{
  "script": "import numpy as np; import sys; nums = list(map(int, sys.argv[1].split(','))); print('Mean:', np.mean(nums)); print('Std:', np.std(nums))",
  "script2": "from fastcore.utils import chunked; print(\"Chunks: \" + str(list(chunked(range(10), 3))))",
  "numbers": [1, 2, 3, 4, 5]
}

Execute Command Node

To run the NumPy snippet:

echo "{{ $json['script'] }}" | python3 - "{{ $json['numbers'].join(',') }}"

Issues encountered

Initially, multi-line Python scripts caused errors due to improper handling of newlines and quotes by the shell. Python snippets containing list concatenations or generator objects required conversion to strings str() before printing.

The main approach was to convert scripts into single-line Python commands or strings with properly escaped newlines \n. Adjusted print statements to handle string concatenations properly str(list(...)). More work needs to be done to make longer scripts more accessible, as using the current method it’s a bit awkard for anything other than fairly small Python scripts.

Issues & Lessons Learned

Key learnings from the process: - PEP 668 enforcement: Python system-package installation restrictions are strictly enforced on Alpine-based containers. The workaround was using --break-system-packages. - Shell scripting nuances: Proper escaping and quoting were essential in Execute Command nodes. Mistakes here led to confusing syntax errors. - Script formatting: n8n requires careful handling of multiline Python scripts. Flattened one-liners or carefully escaped strings worked best. - Package installation: pip proved straightforward for package installations within the controlled environment compared to pipx, which is better suited for installing CLI tools rather than Python libraries for inline scripts.

Practical Example Workflow with Fastcore

Here is an example n8n workflow demonstrating a successful fastcore implementation. I actually implemented the workflow with three Python script executions:

  • Core Python code
  • NumPy code
  • fastcore code.

I’ll just show the fastcore Python code used in this section. You can access the full workflow for the other two Python snippets at the end of the post.

Set Node

{
  "script2": "from fastcore.utils import chunked; print(\"Chunks: \" + str(list(chunked(range(10), 3))))"
}

Execute Command Node

echo "{{ $json['script2'] }}" | python3 -

This correctly printed:

Chunks: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

I added a Discord node to the n8n workflow to output all three Python code snippets to a test Discord channel.

Conclusion

By carefully extending the Docker environment, handling Python system-package constraints, and overcoming shell scripting intricacies, I successfully enabled powerful native Python scripting directly within self-hosted n8n workflows. This approach opens many new possibilities for complex automations, leveraging Python’s extensive ecosystem directly in n8n without external services.

Let me know if you’d like further assistance creating reusable deployment scripts or templates from this experience!

Note: You can download the n8n workflow via my exclusive n8n-showcase Discord channel.

More n8n Content?

If you liked this post please consider following me on Twitter and LinkedIn for more AI content.