Node-RED Pythonshell Raspberry Pi

Hi all,

I would like to run in Node-RED a python script using a python shell block.
After adding the location of the .py script inside the block settings and adding a simple debug window the output is empty.
The location of the py script is in /home/pi

Could you please let me know what I'm doing wrong?


Is the python script working from the command Line?
Maybe you can share the script also with is.

Hi Edje11,

Thanks for your quick reply.
Basically it's just a print command

Hello

Are you sure the wires are connected to the pythonshell node? Try moving it to check!

I see that pythonshell has not been updated for 4 years and it seems to offer little beyond the standard exec node.

[{"id":"5e22caee442ca855","type":"exec","z":"5a97c57f1d60d64e","command":"python /home/pi/hello.py","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":490,"y":400,"wires":[["012e4d81039d268a"],[],[]]},{"id":"7760edc8fca2d9a3","type":"inject","z":"5a97c57f1d60d64e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":400,"wires":[["5e22caee442ca855"]]},{"id":"012e4d81039d268a","type":"debug","z":"5a97c57f1d60d64e","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":400,"wires":[]}]

Add #!/usr/local/bin/python to the top of your script and

Check the box marked Continuous

You shouldn't need the inject node either. (not for this script anyway)

I couldn't get that node to work - gave up in the end. However using the 'exec' node works really well.

Got it working transferring FTP files to/from local and remote servers and run a script on my local Pi.


As an example, I've just added a new part to my flow to add two numbers together using a Python script in the Pi that is running Node-RED. This also shows how to pass parameters/values to the Python script.

Here's a copy of what's in function node 'Run Python script on .152' - the Pi that's running Node-RED.

Note: Depending on the version of Python you are running, you might need to change line-4 to...
msg.payload = "python /home/....etc...
python_B

And here's what's in the Python script located at /home/pi/adder.py

#!/usr/bin/env python3
import sys

a = int(sys.argv[1])
b = int(sys.argv[2])
# c = int(sys.argv[3])
result = a * b
print('Result of multiplying ',a," by ",b, " is ",result)
print(result)

This is how I set-up the 'exec' node.
python_C

Hope this helps to get your hello.py script working.

I don't think that's going to help.

On my Raspberry Pi (Raspberry Pi OS Bullseye) there is no such file as /usr/local/bin/python.

On the other hand It won't do any harm either. The pythonshell node seems to pass the script name as a parameter to python (cf python myscript.py), in which case a hashbang line is ignored.

Thank you a lot for your advices!

Now it's working. It seems that my initial setup was correctly made. After I refreshed the Node-RED webpage it started to work.

2 Likes

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