Giter VIP home page Giter VIP logo

n8n-nodes-python's Introduction

n8n-nodes-python

n8n.io - Workflow Automation

PythonFunction module - custom node for running python code on n8n.

run python code on n8n

Python Function

PythonFunction node is used to run custom Python snippets to transform data or to implement some custom functionality that n8n does not support yet.

Installation

Using n8n-python Docker Image (Recommended)

This node is pre-installed in the n8n-python docker image .

  • Use n8n-python:latest-debian if you are planning to install heavy python packages such as numpy or pandas.
  • Use n8n-python:latest for a more lightweight image.

Example using docker-compose.yml

Adding external packages

You can mount a requirements.txt file to the container to install additional packages.

You can use the ExecuteCommand node to run pip install -r requirements.txt and the n8nTrigger node to trigger it after each restart.

Install Locally

1- Install Requirements

This node requires the following dependencies to be installed in your environment:

  • Python 3.6 or higher

     python3 --version # check output version
  • python-fire

     # install fire
     pip install fire

2- Add n8n-nodes-python to your n8n instance

If you’re running either by installing it globally or via PM2, make sure that you install n8n-nodes-python inside n8n. n8n will find the module and load it automatically.

If using docker, add the following line to your Dockerfile:

# Install n8n-nodes-python module
RUN cd /usr/local/lib/node_modules/n8n && npm install n8n-nodes-python

Read more about the installation process in the n8n documentation - Use the n8n-nodes-module in production .

Usage

This node receives ìtems and should return a list of items.

Example:

new_items = []
for item in items:
		item['newField'] = 'newValue'
		new_items.append(item)
return new_items # should return a list

The JSON attribute of each item is added and removed automatically. You can access the values directly without the json attribute. You don't need to put the item in a json attribute. it will be done automatically.

Variable: items

the items variable is a list of items that are passed to the function. They are directly accessible in the function.

Example:

print(items)
# > list
return items

Credentials: env_vars (optional)

You can specify environment variables to be used in the python code. The environment variables are accessible throw the env_vars dict.

Example:

print(env_vars)
print(env_vars.get('MY_VAR','default_value'))
# > dict

Logging to the browser console

it is possible to write to the browser console by writing to stdout

Example:

print('Hello World')
# or
import sys
sys.stdout.write('Hello World')

Notes

  • stderris used for passing data between nodes.

    • if exit code is 0, the node will be executed successfully and stderr represents the JSON representation of the output of the node
    • if exit code is not 0, the node fails and stderr represents the error message
  • The json attribute of each item is added and removed automatically. (you can access and return the items directly without the json attribute)

Contribute

Pull requests are welcome! For any bug reports, please create an issue.

License

Apache 2.0 with Commons Clause

n8n-nodes-python's People

Contributors

ariarijp avatar janober avatar lublak avatar naskio avatar ozee31 avatar thiagopxp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

n8n-nodes-python's Issues

ERROR: exitCode: 0 spawn python3 ENOENT

Hi!

I'm testing this node but the 'ERROR: exitCode: 0 spawn python3 ENOENT [Line xxx]' cames on every code tested.

For example:

new_items = []
for item in items:
		item['newField'] = 'newValue'
		new_items.append(item)
return new_items # should return a list
print(items)
# > list
return items

n8n: v0.224.1
n8n-nodes-python: v0.1.4

It seems to be a problem with the path on the node.js code:
https://stackoverflow.com/questions/55033450/spawn-python-enoent-node-js-in-windows
https://stackoverflow.com/questions/58390884/how-to-fix-error-spawn-py-enoent-error-when-running-python-script-from-electr
https://stackoverflow.com/questions/47099422/error-spawn-python-enoent

No Python Node

Hi There

After installing naskio/n8n-python:latest via docker-compose following the requirements I did not find the "Python Node"
image

Am I missing something?

Note:
My n8n is running using Postgres and everything else is working 100% as intended.
I also currently have no "Scripts" in the python scripts folder as I first want to see how everything works.

Report a bug in PythonFunction.node.ts

hi Naskio,

I encountered a troublesome issue in the n8n.
I think it might be a bug in this code

In line 195, the code 0 represent success, but the stderr will be parse.

if (!code) { returnData.items = parseShellOutput(returnData.stderr); } else { returnData.error = new Error(returnData.stderr); }

Error when parsing output of PythonFunction

image

According the error message, the output stderr contains invalid token /. But I am sure that the code in the PythonFunction.node is correct.

How can I get the stderr of PythonFunction.node

Error fetching feature flags ReferenceError: AbortSignal is not defined

Hello, I have an error message Error fetching feature flags ReferenceError: AbortSignal is not defined when using PythonFunction. The process freezes for 10 seconds, and the result of the test script is displayed correctly, but this error occurs in the log.

Can it come from my nginx proxy manager reverse proxy?

Error 1:

Error fetching feature flags ReferenceError: AbortSignal is not defined
    at PostHog.<anonymous> (/usr/lib/node_modules/n8n/node_modules/posthog-core/src/index.ts:460:6)
    at step (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:102:23)
    at Object.next (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:83:53)
    at /usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:76:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:72:12)
    at PostHogCoreStateless.fetchWithRetry (/usr/lib/node_modules/n8n/node_modules/posthog-node/lib/index.cjs.js:1063:16)
    at PostHog.<anonymous> (/usr/lib/node_modules/n8n/node_modules/posthog-core/src/index.ts:219:17)
    at step (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:102:23)
    at Object.next (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:83:53)
    at /usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:76:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:72:12)
    at PostHogCoreStateless.getDecide (/usr/lib/node_modules/n8n/node_modules/posthog-node/lib/index.cjs.js:833:16)
    at PostHog.<anonymous> (/usr/lib/node_modules/n8n/node_modules/posthog-core/src/index.ts:318:39)
    at step (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:102:23)
    at Object.next (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:83:53)
    at /usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:76:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:72:12)
    at PostHogCoreStateless.getFeatureFlagsAndPayloadsStateless (/usr/lib/node_modules/n8n/node_modules/posthog-node/lib/index.cjs.js:950:16)
    at PostHog.<anonymous> (/usr/lib/node_modules/n8n/node_modules/posthog-core/src/index.ts:306:24)
    at step (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:102:23)
    at Object.next (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:83:53)
    at /usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:76:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/lib/node_modules/n8n/node_modules/node_modules/tslib/tslib.es6.js:72:12)
    at PostHog.call [as getFeatureFlagsStateless] (/usr/lib/node_modules/n8n/node_modules/posthog-node/lib/index.cjs.js:937:16)
    at PostHog.capture (/usr/lib/node_modules/n8n/node_modules/posthog-node/src/posthog-node.ts:106:38)
    at PostHogClient.track (/usr/lib/node_modules/n8n/src/posthog/index.ts:39:17)
    at /usr/lib/node_modules/n8n/src/telemetry/index.ts:193:20
    at new Promise (<anonymous>)
    at Telemetry.track (/usr/lib/node_modules/n8n/src/telemetry/index.ts:177:10)
    at InternalHooks.onWorkflowPostExecute (/usr/lib/node_modules/n8n/src/InternalHooks.ts:371:22)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Error 2:
Could not initialize license manager sdk

Test python script:

new_items = []
for item in items:
		item['newField'] = 'newValue'
		new_items.append(item)
return new_items # should return a list

I use this docker-compose file with portainer:

version: '3.8'

services:
  n8n:
    image: naskio/n8n-python:latest-debian # use this if intended to use heavy python packages
    #    image: naskio/n8n-python # alpine for light python packages
    #    command: /bin/sh -c "n8n start"
    restart: always
    container_name: n8n
    environment:
      - GENERIC_TIMEZONE=Europe/Moscow
      - TZ=Europe/Moscow
      - N8N_HOST=n8n.[HIDE DOMAIN]
      - N8N_PROTOCOL=https
      - N8N_PORT=443
      - VUE_APP_URL_BASE_API=https://n8n.[HIDE DOMAIN]/
      - WEBHOOK_TUNNEL_URL=https://n8n.[HIDE DOMAIN]/
      - N8N_EDITOR_BASE_URL=https://n8n.[HIDE DOMAIN]/
      - DB_TYPE=mariadb
      - DB_MYSQLDB_HOST=mariadb
      - DB_MYSQLDB_PORT=3306
      - DB_MYSQLDB_DATABASE=n8n
      - DB_MYSQLDB_USER=n8n
      - DB_MYSQLDB_PASSWORD=[HIDE PASSWORD]
    ports:
      - "127.0.0.1:5678:443"
      
    volumes:
      - /home/n8n_data:/home/node/
      - /home/n8n_data/local-files:/data # by default workdir == /data
      - /home/n8n_data/python_scripts:/data/py_scripts
      - /home/n8n_data/requirements.txt:/data/requirements
      
volumes:
  n8n_data:
  
networks:
  default:
    external: true
    name: evilcorp

Error when importing workflow.json

Hi, I'm getting the following error when importing workflow.json and the build is failing:

An error occurred while importing workflows. See log messages for details.
Cannot update entity because entity id is not set in the entity.

This only occurs with the more recent packages, I have reverted to using 0.210.2-debian and the build works.

Can't run python with npm package

Hi,
I install package with community package on n8n via : https://www.npmjs.com/package/n8n-nodes-python
i set up creadential like this : (i don"t know if the right way)
image
Then if i run python command it won't work :
image
cant find python path ?

If i run execute node with python command is working.
image

n8n is install on windows 10 machine with npm, following this step : https://docs.n8n.io/hosting/installation/npm/
Latest n8n
Latest nodejs
Running on windows 10 and python 3.9 is install and working.

Getting OOM error while running python node

getting the following error: n8n may have run out of memory while executing it while executing an HTML parsing job.

n8n was assigned 20g ram, so there is no way the execution on this node would run oom.
I wonder if there is a memory limit on the node itself. How can I config this limit?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.