Python in Node Red - RaspberryPi

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

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:

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

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

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?

1 Like

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).

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

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. 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?

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!?

1 Like

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

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.


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

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

@krambriw whats the input of your exec_node ?

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

1 Like

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


searching in google it appears missing some kind of package

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/"

1 Like

Well it works, a bit

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

  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
  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?

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.

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

I try to send the files trough this comment but it says Im to young in this forum :sweat_smile:

I put all my scripts in we transfer:

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?

1 Like

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

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

1 Like

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 :slightly_smiling_face: