# Python in Node Red - RaspberryPi

**URL:** <https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888>\
**Category:** General\
**Created:** [11 July 2020 10:19 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888 "2020-07-11T10:19:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [11 July 2020 10:19 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/1 "2020-07-11T10:19:08Z")

</div>

Hello everyone,

Im starting in Node Red and I want to known if this is possible:

I done some scripts in python and their general aspect is like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/26a910a6891ff2441701a01e561b91c7b9dec64a.png)

Like you can see I have a menu, and when a user choose is option I comunicate with i2c devices and others protocols.

I want to know if this is possible in Node Red.  
Like you may realize this need to be in real time and fast.  
I try to run one of my scripts using pythonshell but it gives me an error.

I will leave some videos that i do, to you have some ideas:  
https://www.youtube.com/embed/ckHUtJxlUdA?feature=oembed&wmode=opaque&list=PLkW5GG7wE8EI2dvLTMzjECLvhfdUsL-_J&start=0

Its possible to run a script like this in Node-Red ?  
Thanks

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [11 July 2020 17:20 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/2 "2020-07-11T17:20:15Z")

</div>

Well,,,you can start/run a python script using the Exec node and it will then show your menu in a window like that. But you have to tell that the script shall run in a window on the display. Otherwise it will be hidden from the display

I do start my python scripts this way, I give you an example how to do:

In the picture below I have 4 different python scripts doing different things. I installed xterm first because it gives me more control of windows size and position on display etc

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

The syntax for the command is as example below. But your menu is not so good looking, right? Try make your users more happy, in Node-RED you can create a nice dashboard for your menu instead and send user choice as parameter to your python script instead, I think you should try this?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/680899740d001fc3def7be7c2ba5dc1cb9b30247.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 July 2020 17:25 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/3 "2020-07-11T17:25:45Z")

</div>

We also have the node-red-node-daemon which is like the exec node but for long running processes - you can feed inputs via stdin and get back stdout and stderr - so you may be able to drive menus that way also (if that isn't just a one off operation).

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [11 July 2020 17:33 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/4 "2020-07-11T17:33:07Z")

</div>

Thank you both ... I will try and I will give some feedback

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [11 July 2020 17:59 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/5 "2020-07-11T17:59:35Z")

</div>

Hey guys,  
I don't use python myself, but I thought you would have advised Marcos to start using the [node-red-contrib-python-function](https://flows.nodered.org/node/node-red-contrib-python-function). Seems at first sight a very easy to use node, similar to the Javascript function node which we use all over the place... Or does python work completely different?  
Bart

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [11 July 2020 18:07 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/6 "2020-07-11T18:07:40Z")

</div>

It depends what is in the python script. Not everything works in the node. And the python script will run non-visible, no menu for users will be shown!?

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [11 July 2020 18:15 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/7 "2020-07-11T18:15:44Z")

</div>

If you watch the video above ... you guys will see whats inside the script, and that I need a menu...

So modify the python script so it can receibe inputs may be a option

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [12 July 2020 08:49 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/8 "2020-07-12T08:49:52Z")

</div>

> [@BartButenaers](#):
>
> using the [node-red-contrib-python-function](https://flows.nodered.org/node/node-red-contrib-python-function). Seems at first sight a very easy to use node, similar to the Javascript function node which we use all over the place... Or does python work completely different?

If you read the notes it’s saying “only use this in sandbox, not production ready”. And counting the number of issues I’ve seen with it on the forum that I had to assist on, I’ve to agree with that. The pythonshell node is arguably better. The python function nodes start a script with the contents of the function in the background, then attempt to serialise the incoming message contents and send them to the running python process, where it’s unserialised again. While an understandable approach, the python code that handles this isn’t properly set up with error handling to report back to the user what went wrong, and how/why to solve this.  
I’m adding here that as a Python expert user I’m able to provide support but only because I’m familiar with core modules and where certain parts of stacktraces are likely caused by. If anything with the code you put in this node or messages you feed it goes wrong, you’re basically on your own unless you know you’re way around in the Python core to debug the underlying script that powers the node.

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [12 July 2020 14:46 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/9 "2020-07-12T14:46:56Z")

</div>

Hello everyone,  
Thank you all for your answers and ideias it helps a lot, in this project to my school.

So, the feedback is:  
I try with daemon node and it keeps sending error

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/263f3a93790558920b78c3ca88182cd2e5adc0f0.png)

then I try the solution of @krambriw because it seems very logical but it gives error again ... however i think that the error might be related with the input of exec\_node .... i dont know, just see

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53117878be1d0e66953209bc809f889626d94d20.png)

@krambriw whats the input of your exec\_node ?  
Thanks

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 July 2020 15:05 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/10 "2020-07-12T15:05:36Z")

</div>

Normally I just send bool (true) as input but it should not make difference unless you want to send specific parameter to the script

I think this error is because of DISPLAY=:1, maybe try DISPLAY=:0 instead

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [12 July 2020 18:32 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/11 "2020-07-12T18:32:03Z")

</div>

It almost did it ... the window open and close at the same time and in Node appear this error

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/2522e891eb66086dcd510e52eb9bd2974d9d4f4d.png)

searching in google it appears missing some kind of package

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [12 July 2020 20:53 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/12 "2020-07-12T20:53:22Z")

</div>

Yes a font obviously. I do not now about that, maybe you have a system font defined for yoiur country

But anyway, try then instead this syntax using the standard lxterm

`export DISPLAY=:0 && lxterminal -e "python3 /home/pi/yourscript.py"`

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [12 July 2020 21:44 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/13 "2020-07-12T21:44:45Z")

</div>

Well it works, a bit

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

The window appears, despite of not run the script inside .... I have now another question:

There's any way of, instead open a terminal to run the script, run the script itself in a widget or something fixed in the webpage, like a display ?? .... I don't know

Thank you for your replies, it help's a lot

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [13 July 2020 04:34 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/14 "2020-07-13T04:34:02Z")

</div>

1. Something is blocking the script to continue, maybe something also related to the missing fonts

- how did you run the script manually? Did you click on it to start it or what?
- have you tried to run the script manually from the terminal?
- you can also try to configure the exec node like this, in spawn mode, to see if there is any change
- but I do not know what is inside your script so I cannot debug. If you make a simple version of your script or share it, I can test it myself  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/6/d60f6c1d5e809001e7ed42d25d6b8bf0c69625db.png)

1. I do not think you can embed a terminal display like that into a webpage. Maybe someone else knows a trick but if you want to make a web gui, I think the script needs re-design. The main functionality is most likely what you keep but you need to provide the script to take inputs from users like you have already in menu, "Pick an option", but now instead from buttons or dropdown menu. Then you have to display the result to user in the web page, to do this the script needs to return the result via websocket so the webpage can be updated dynamically.

I could see a solution structure like this:  
dashboard menu -\> user selection to script -\> script execution -\> script result -\> via websocket -\> dashboard presentation of result

If the user needs to interact more in between, like the script needs some more input from user while it is executing, everything becomes more complicated

What is your use case, why you wanna run the script from Node-RED? It seems to be working fine as is. Is it used for testing pcb board that you manufacture and sell to customers?

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [13 July 2020 16:14 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/15 "2020-07-13T16:14:00Z")

</div>

This is for my final university project, I have some PCB, used in my workplace, and the comunication is mainly I2C or RS232 or SPI.  
I used Python to do scripts, user frindly, test the PCB. Each PCB has a different python script and they share some functions, to simplify the work.

And well I just want to know if Node Red can do it.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a83e82f21d62720eca9cbe83e9b5e4a1a5264a4.png)

Normally I run my programs just like that:  
But as you can see ... its not a nice GUI as

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/702b6794eeaf848367c435dc026926764483e609.png)  
I try to send the files trough this comment but it says Im to young in this forum 😅

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [13 July 2020 16:38 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/16 "2020-07-13T16:38:42Z")

</div>

I put all my scripts in we transfer:

> **[ADC.py and 8 more files](https://wetransfer.com/downloads/58b4162f956a0474a15b262636b509f520200713163722/bbd580)**
>
> 9 files sent via WeTransfer, the simplest way to send your files around the world

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [13 July 2020 17:41 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/17 "2020-07-13T17:41:26Z")

</div>

I cannot run it anyway in my systems, we seem to have so different character sets & language support installed

But anyway, just some questions? I suppose the functionality is working as you wish? So the python code is fine but not looking "too attractive"? Is that the point?

Well if so, Node-RED is not suited to make a "facelift" of an existing character based software. There are development tools for python gui applications if that is what you really want. But it will be quite a lot work too.

Node-RED could be used as I described earlier but then the python script has to be "stripped off" from display & user inputs via screen. You could then have the python script as a "service" doing it's stuff in the background and report the result back. But also for this, pretty lot to do.

So question is, is the work worth the effort?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b8a63a85a263ef2195ed91b8a617ed0813c62d89.jpeg)

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [13 July 2020 17:55 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/18 "2020-07-13T17:55:55Z")

</div>

Thnks for the enlightenment .... of course the work worth the effort but, this in the future will grow and i will have more scripts, and maybe do a database .... so it cant be to much hard to implement the code in the GUI

Maybe i will turn on PyQT again (in first i try but was hard, for someone who doesnt know to much programing ... just C and Python) or Tkinter

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [13 July 2020 18:05 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/19 "2020-07-13T18:05:08Z")

</div>

Python and C is very valuable to be skilled in! Thinking about the future, for you now as student, so much is happening in AI, Python is heavily used there with OpenCV for image analyze and C (and also Cython in some cases) for performance. Self driving taxi will be launched here later this year. You have an interesting future!

I have done so much in Python, I think started 2006. With Node-RED again a change, now focus on javascript and html. I could re-use many of my Python working packages as services and I was hoping to stay on this high level. Until I could not stay away from the small device ESP32, I had to dive down into C again, life is a challenge

---

<div class="post-metadata">

**Author:** ![MarcosF](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MarcosF](https://discourse.nodered.org/u/MarcosF)\
**Post date:** [13 July 2020 18:12 UTC](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888/20 "2020-07-13T18:12:07Z")

</div>

Thanks a lot ..... I will do some Testing in a script of python receive inputs from Node ... And then I will, maybe and if I have time, modify my scripts to integrate in Node .... Node seems to be very easy to use

Thanks 🙂

[Next page](https://discourse.nodered.org/t/python-in-node-red-raspberrypi/29888.md?page=2)
