Running Python with n8n

Hi n8n community,

We have some Python files that we would like to run on a regular basis, and I was thinking of using n8n for this: A Cron node followed by an Execute command node to run a specific Python file.

Is this possible, and if so does anyone have any insights that could help us deploy such a solution? We have deployed n8n on Heroku, maybe there are some specifics? Such as how to install Python and all necessary libraries.

Also, once we have the Python file that we have developed locally, should we use an FTP to send it to our n8n database or can this be done directly via n8n?

Many thanks,
Maxim

1 Like

Hi @maximp , that’s totally doable as you can see in the screenshot. Basically the execute command node is a bash instance where you can execute any available command.

To achieve what you are aiming for, you need to have python installed on your instance with the required libraries you will be using.
Also you can use the execute command node to install them, just make sure you have the right user (with the right privileges) when starting then n8n instance.

Concerning the files, it is better to save them directly on the file system, rather than using n8n database, also you can use a combination of FTP and writeBinaryFile node to retrieve and save the files before executing them.

2 Likes

Btw. if you want to build a docker-image with python you can extend the default n8n image and extend it.

For that create a file name Dockerfile with this content:

FROM n8nio/n8n
RUN apk add --update python3 py3-pip

And then run in the same folder the following command to build it:

docker build -t n8n-python .

If you then use this image, you will be able to run python scripts (be aware that you would have to use the command python3 and not python).

3 Likes

Thanks @jan,
Is it also possible to add python to a docker-compose file? I use docker-compose to deploy n8n and traefik but would love to add python support as well.

Yes, @chris! The docker-compose file just describes how what docker images to use and with what parameters. So you would do exactly what I did explain above and then change the docker-image you use in the docker-compose file.

So from something like:

...
  n8n:
    image: n8nio/n8n
...

to:

...
  n8n:
    image: n8n-python
...

Thanks @jan.

To understand completely:
image: n8nio/n8n uses official n8n image from the docker registry, while
image: n8n-python uses the image I locally build (which uses official n8nio/n8n, just with python)?

I know this is a Docker specific question, so appreciate your support here.

Yes, that is 100% correct.

It is however important to remember that you would have to build a new custom image every time a new n8n version gets released.

So would be best to use a Dockerfile like this:

FROM n8nio/n8n:0.119.0
RUN apk add --update python3 py3-pip

in which you specify the exact version of the n8n image you want to use. After that has then be build you would have to do:

# Stop the currently running containers
docker-compose stop 
# Remove them so that it has to recreate them and does not reuse the old ones
docker-compose rm
# Startup n8n again
docker-compose up -d
1 Like