# Node-RED Pythonshell Raspberry Pi

**URL:** <https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 August 2022 21:59 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990 "2022-08-03T21:59:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandru89](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alexandru89](https://discourse.nodered.org/u/Alexandru89)\
**Post date:** [3 August 2022 21:59 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/1 "2022-08-03T21:59:37Z")

</div>

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?

 ![Pic1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31c4fe78128e8e343669a67e78d66d77c45fd251.png)  
 ![Pic2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/6560f0ea304b2dea5c1373be62eaf9b93e209099.png)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [3 August 2022 22:43 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/2 "2022-08-03T22:43:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexandru89](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alexandru89](https://discourse.nodered.org/u/Alexandru89)\
**Post date:** [3 August 2022 22:58 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/3 "2022-08-03T22:58:32Z")

</div>

Hi Edje11,

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

![Hello](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d499a60dc8229e2628f2a05fd9e5bdee6eeb8bb1.png)

 ![Hello2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c690ccf78837549ccfde8af860d5a7d18ca359a9.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 August 2022 23:31 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/4 "2022-08-03T23:31:57Z")

</div>

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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [4 August 2022 00:23 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/5 "2022-08-04T00:23:43Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 August 2022 05:41 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/6 "2022-08-04T05:41:39Z")

</div>

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.

 ![python_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80f0f7850fea5627d47ce2f5c1afb82415a7e460.png)  
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdb0d9de76980ab21bff1259bdd6d66745cc407b.png)

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

```auto
#!/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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/d/9d1266ca6efac57884800fa780bed65693ef6427.png)

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 August 2022 08:25 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/7 "2022-08-04T08:25:23Z")

</div>

> [@HaroldPetersInskipp](#):
>
> Add `#!/usr/local/bin/python` to the top of your script and

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.

---

<div class="post-metadata">

**Author:** ![Alexandru89](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Alexandru89](https://discourse.nodered.org/u/Alexandru89)\
**Post date:** [4 August 2022 10:41 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/8 "2022-08-04T10:41:52Z")

</div>

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.

 ![Hello3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c730d6b718706e04491e858e4fa3f07d2994983.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 August 2022 10:42 UTC](https://discourse.nodered.org/t/node-red-pythonshell-raspberry-pi/65990/9 "2022-08-18T10:42:53Z")

</div>

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