How to mport a python script

Hi there.
I want to use a whole python script I wrote (or "just" the functions) in node-red.

How to do that?

Tutorials I found were quite confusing.
Thanks you for help.
Your beg1nner

You can use an exec node to execute an external python script.
There is also a python node for node-red.

Question is, do you really need python ? Most things can be done in javascript as well as both languages can do similar things.

How to do it with the exec-node?
And where can I find the python-specific-node?

Make sure to go through the documentation. install nodes

If your python script is an executable file you can run it with the exec node, passing any arguments in msg.payload.

If you prefer the python code to be stored inside your Node-red flow, here is an example showing how.

[{"id":"3f931e928d8bc3f3","type":"group","z":"db04fd79f3a93d19","name":"Executing a python script ","style":{"label":true},"nodes":["9761974cbd7f81d2","c4d009099ef709e3","20d8c64ff458227e","3b0d9776ffb8d484","3beba92f8eb5a185"],"x":14,"y":831.5,"w":832,"h":149.5},{"id":"9761974cbd7f81d2","type":"inject","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Pass values in msg.payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4.2 Python\\ script\\ stored\\ in\\ Node-red 99","payloadType":"str","x":180,"y":880,"wires":[["c4d009099ef709e3"]]},{"id":"c4d009099ef709e3","type":"template","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Python script","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"python -c '            # -c argument - accept command inline. Must use single quotes.\nimport sys             # Process argument list\nnum1 = float(sys.argv[1])\nstr = sys.argv[2]\nnum2 = float(sys.argv[3])\nprint (\"1st argument is \", num1)\nprint (\"2nd argument is \", str)\nprint (\"3rd argument is \", num2)\nprint()\nif(num1 <= 10):\n  print (num1, \"is less than 10\")\nelse :\n  print (num1, \"is greater than 10\")\nif (num2 <= num1):\n  print (num2, \" is less than \", num1)\nelse :\n  print (num2, \" is greater than \", num1)\n' {{payload}} # Closing single quote followed by list of arguments\n","output":"str","x":390,"y":880,"wires":[["20d8c64ff458227e","3b0d9776ffb8d484"]]},{"id":"20d8c64ff458227e","type":"exec","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":550,"y":880,"wires":[["3beba92f8eb5a185"],["3beba92f8eb5a185"],[]]},{"id":"3b0d9776ffb8d484","type":"debug","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Full Script","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":940,"wires":[]},{"id":"3beba92f8eb5a185","type":"debug","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"debug 299","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":880,"wires":[]}]
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.