Passing parameter from node-red to python

Hello,

I would like to pass parameter from Node-red to python script.
1.Is it possible to do that using: https://flows.nodered.org/node/node-red-contrib-pythonshell

Python script would be:

a={Passed parameter}
b=2

print(a*b)
  1. Have also tried to understand: Passing (msg.payload) in exec node to a python script function without success

how to define script path in windows? In my case: C:\Users\admin\PycharmProjects\TEST\script.py

You can pass the extra parameter's as a command arg or from standard-in (what ever works better for your flow). The test files for that node give you an example of each:

From command arg: https://github.com/namgk/node-red-contrib-pythonshell/blob/master/test/sample-with-arg.py

From Standard in: https://github.com/namgk/node-red-contrib-pythonshell/blob/master/test/stdin-data.py

Thank you,

so dumb of me to miss github for examples... Anyway

import sys

while True:
	line = sys.stdin.readline()
	print line

I get error

File "C:/Users/admin/PycharmProjects/TEST/debug.py", line 5
    print line
             
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(line)

when I try:

print(line)

I get no output

[{"id":"5420ad3a.8a1b44","type":"pythonshell in","z":"8397e515.d35b18","name":"","pyfile":"C:\\Users\\admin\\PycharmProjects\\TEST\\debug.py","virtualenv":"","continuous":false,"stdInData":false,"x":341.5,"y":154,"wires":[["23eadfc5.92c34"]]},{"id":"95914516.3e1af8","type":"inject","z":"8397e515.d35b18","name":"","topic":"","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":"","x":130.5,"y":76,"wires":[["5420ad3a.8a1b44"]]},{"id":"23eadfc5.92c34","type":"debug","z":"8397e515.d35b18","name":"","active":true,"console":"false","complete":"false","x":537.5,"y":233,"wires":[]}]

And code is in infinite loop.

Any ideas?

print line verse print(line) is python 2 verse python 3. With python3 the print statement has been replaced with a print function.

As for the code being in an infinite loop: yes ... that's what that example does. It's an infinite loop that accepts a line of input from standard-in and prints it out to standard-out.

Your flow seems to be working perfect. There's a script that starts running ... everything that get's injected goes into the script via standard-in and gets replayed back out via standard-out. If you want the script to run once for each payload you would need to edit the python node and turn off "Continuous" and pass the data as an argument instead.

Here's an example of the script running once for each payload and reading the payload of the argument:

Python 2 code:

import sys
print 'Hello ' + sys.argv[1]

Flow:

[{"id":"2c4b9956.371766","type":"inject","z":"f549c99a.345c38","name":"","topic":"","payload":"KarolisL","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":170,"y":520,"wires":[["1919c9a5.8929a6"]]},{"id":"e70314d0.69e5e8","type":"debug","z":"f549c99a.345c38","name":"","active":true,"console":"false","complete":"false","x":590,"y":520,"wires":[]},{"id":"1919c9a5.8929a6","type":"pythonshell in","z":"f549c99a.345c38","name":"","pyfile":"sample-with-arg.py","virtualenv":"","continuous":false,"stdInData":false,"x":380,"y":520,"wires":[["e70314d0.69e5e8"]]}]

This ones once rather than a continues loop

1 Like

Sorry for dumb question, but I only managed to solve it by running this code:

import sys

a = int(sys.argv[1])

print (2*a)

I receive 40 in debug node.

When I run:

import sys

line = sys.stdin.readline()
print (line)

I get still don't get output from pythonshell.

Bonus question.. Is it possible to pass object

a ={};
a.val1=1;
a.val2=2;

msg.payload=a;
return msg; 

as msg.payload to python script and how to extract those values in the script?

Question was answered by googling...

It is best to use json node

[{"id":"45771f1f.8f26e","type":"function","z":"8397e515.d35b18","name":"","func":"a ={};\na.val1=1;\na.val2=2;\n\nmsg.payload=a;\nreturn msg;","outputs":1,"noerr":0,"x":233.16669464111328,"y":432.66666984558105,"wires":[["572f1be0.01ef04"]]},{"id":"c0f867b2.7f5698","type":"inject","z":"8397e515.d35b18","name":"","topic":"","payload":"KarolisL","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":136.00000762939453,"y":345.00000953674316,"wires":[["45771f1f.8f26e"]]},{"id":"d7e850ac.ed588","type":"debug","z":"8397e515.d35b18","name":"","active":true,"console":"false","complete":"false","x":614,"y":345,"wires":[]},{"id":"d74a5980.dd7fe8","type":"pythonshell in","z":"8397e515.d35b18","name":"","pyfile":"C:\\Users\\KL\\PycharmProjects\\TEST\\debug.py","virtualenv":"","continuous":false,"stdInData":false,"x":404,"y":345,"wires":[["d7e850ac.ed588"]]},{"id":"572f1be0.01ef04","type":"json","z":"8397e515.d35b18","name":"","property":"payload","action":"str","pretty":true,"x":392.16668701171875,"y":446.66668701171875,"wires":[["d74a5980.dd7fe8"]]}]

Answered in Passing JSON object (as string) to python script (exec node)