Passing an input to exec node which execute a python script

hi ,

i have a python script (.py) which i can execute in node-red easily. but i need to pass a json string to my python script as an input. in another word the python script which is on my local drive should read the input from json string in node red and then print the result in message payload. here is my flow

[{"id":"a4393e63.8acd2","type":"exec","z":"e5623cad.9681e","command":"python C:/Users/sr/Desktop/Untitled2.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":396.5,"y":191.50000381469727,"wires":[["1f1b368b.04c419"],[],[]]}]

thanks in advance

Please see this FAQ entry for how to share your flow. You can edit your previous post to correct it so that it becomes importable.
In the meantime you can append the payload to the command in the exec node so you can pass the string to your script if that is what you mean. If that isn't what you mean then please explain further.

In addition to what Colin explained, you have to handle the arguments passed on to the script correctly. (You might already have handled this)

I use argparse in my python scripts. Like this as example:

import argparse
 
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
    "-i", 
    "--images", 
    required=True, 
    help="path to images directory"
)
ap.add_argument(
    "-n", 
    "--nbrs", 
    required=True, 
    help="number of pictures to send per event"
)
ap.add_argument(
    "-c", 
    "--conf", 
    required=True, 
    help="required confidence level from analyze"
)
args = vars(ap.parse_args())

Then to use the arguments later in the code:

path = args["images"]
max_send = int(args["nbrs"])
conf = float(args["conf"])

you have add payload unticked - if you tick that then msg.payload will be added to the command line sent to the command as a parameter.

Or if you mean you have a long running python process and need to pass in things to STDIN then you probably need the node-red-node-daemon instead as that provides stdin and stdout to a running process.

(Note for python you may need to add a -u parameter to stop it buffering the output and to send it through immediately)

IMHO if you need this its easier and more flexible to use MQTT to communicate from node-red to your Python code and vice-versa. The Python paho-mqtt library is well documented and the mosquitto MQTT broker is easily run on your localhost (unless its Windows), Node-red makes MQTT easy to use.

Well, I do agree that using MQTT for communication between NR and python (or whatever that is supporting MQTT) is superior to most (tell me, I'm using it all over the places)

But it would require that the python script is already running and able to listen to incoming messages. In this particular case I assume that the author of the topic wished to run a script, pass params to it and then the script would terminate when execution finished

For sure you could create a flow that starts the script, waits for acknowledgement from the script via MQTT that it is ready and then sends the foreseen related parameters with values. But maybe this becomes too complicated if you just want to run a script that terminates when execution is finished

JFI - I've been running a mosquitto broker on my Win10 machine for 3 years without any trouble at all :slight_smile:

I'm not a big Windows guy, getting off the bus when Windows 8 came out, but my comment was based on helping a friend.

It appeared from my Googling, that mosquitto for Windows required Cygwin to be installed. This was more than he wanted to bite off, and since he had a Raspberry Pi2 sitting idle I suggest using it as the Broker instead.

If there is a windows installer for mosquitto, I'd appreciate the link, as I didn't find it. I like Cygwin, but it can be a challenge for a Windows user with no Linux/Unix experience.

By comparison it was trivial to get Python, OpenCV, nodejs and node-red installed and working on Windows 10. Windows 7 had issues with a missing DLL with the OpenCV (pip install), I was able to solve it with difficulty as there are at least five versions of that DLL kicking around and only one worked for me.

I installed it a few years ago but current installer exe seems to be available here

Maybe it was a bit more manual in the old days of Win8 :slight_smile:

Many thanks, don't know why I didn't find it in my searches, but my friend will be grateful as having a self-contained system is an advantage for him.

duly noted, thanks

thanks , it worked :+1: