How to Run Python Code in n8n: A Step-by-Step Guide

Learn how to install and use Python code in n8n to run custom Python snippets, transform data, and implement custom functionality.

Published
July 21, 2023

Python is an object-oriented, high-level, interpreted language with dynamic semantics. Python is frequently used for creating websites and applications, automating repetitive tasks, and analyzing and displaying data, not to mention that it can be useful in your automation too. Let‘s see how you can run Python in n8n. We will explain two common ways of running Python.

The First Way: PythonFunction Node

n8n is a popular open-source workflow automation tool that enables users to connect different applications and automate workflows without writing a single line of code. If you are looking to run custom Python snippets to transform data or implement some functionality that n8n does not support yet, the PythonFunction node is your go-to tool. In this article, we will show you how to install and use the PythonFunction node to run Python in n8n.

Installation

Using the n8n-python Docker Image is the recommended way to install the PythonFunction node, as it comes pre-installed in the image. Depending on your use case, you can choose between two variants of the image. If you are planning to install heavy Python packages such as numpy or pandas, use n8n-python:latest-debian. For a more lightweight image, use n8n-python:latest.

To add external packages, you can mount a requirements.txt file to the container and use the ExecuteCommand node to run pip install -r requirements.txt. The n8nTrigger node can then trigger it after each restart.

If you prefer to install n8n and the PythonFunction node locally, you will need to install Python 3.6 or higher and the python-fire package. After that, you can add the n8n-nodes-python module to your n8n instance.

Way 2: 0CodeKit Python Endpoint

We offer a way simpler method to run Python code in n8n, thanks to our Python endpoint!

Here you can find out how to connect 0CodeKit to n8n-super easy, trust me!

Running Python code with 0codekit involves a few specific steps. First, you must establish a connection and locate the Python code node within the resource section, specifically under the 'code' category. Despite some initial limitations, such as the inability to directly select Python, the platform's comprehensive documentation provides valuable assistance.

When executing Python code in 0codekit, the most crucial aspect to keep in mind is that you must include a specific structure when defining your functions. Any function that returns a value must include a 'result data' section where you substitute 'your variable' with the name of your function. This serves as the function's return statement.

For instance, consider a function to multiply two numbers. Your function might take two parameters, number 1 and number 2, and return their product. This result data calculation would then be assigned to the function's result.

Inputting dynamic parameters is also possible with 0codekit. Using the 'set node', you can attach parameters to an existing object. For example, you could use the set node to assign number 1 and number 2 to the multiplication function, thereby making these parameters dynamic. The function, defined as a string, is then added to the 'code' field of the object.

Finally, you can call the function, providing it with the relevant parameters, which could be variable inputs from an external source. Upon inspection, you should retrieve the Python string with the assigned values and see the execution result. The dynamic nature of this setup enables easy modification of input parameters and facilitates complex code executions. Remember, the key to success is diligently referencing the provided documentation and understanding the platform's specific coding requirements.

n8n Wrokflow

Run your Python Code Today with 0CodeKit

In conclusion, running Python code with 0codekit is a flexible and dynamic process. Despite some initial challenges, including the inability to directly select Python and the need to thoroughly understand the platform's specific coding requirements, the platform offers a robust, adaptable environment for code execution. Leveraging its comprehensive documentation, users can define and manipulate functions, use dynamic parameters, and execute complex codes. By using the 'set node', users can create dynamic inputs and attach them to existing functions, enhancing the versatility of their code. 0codekit provides a unique platform that, while requiring a certain level of familiarity, ultimately offers great flexibility for those looking to run Python code.

What’s a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.