# NodeRED and python

**URL:** <https://discourse.nodered.org/t/nodered-and-python/15517>\
**Category:** General\
**Created:** [13 September 2019 15:58 UTC](https://discourse.nodered.org/t/nodered-and-python/15517 "2019-09-13T15:58:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 15:58 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/1 "2019-09-13T15:58:07Z")

</div>

How can we get the output from a python file which is run not using exec node from the editor into NodeRED ?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [13 September 2019 16:31 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/2 "2019-09-13T16:31:17Z")

</div>

there are MQTT libraries for python, so MQTT is one option

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 16:44 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/3 "2019-09-13T16:44:29Z")

</div>

@ukmoose anything else using, stdout ?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [13 September 2019 16:54 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/4 "2019-09-13T16:54:51Z")

</div>

output to tcp/udp port and listen for the port in NR  
or write to a file and read the file in NR ?

---

<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:** [13 September 2019 17:23 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/5 "2019-09-13T17:23:16Z")

</div>

> [@ALOKA1432](#):
>
> which is run not using exec node from the editor into NodeRED ?

Have you figured out yet why it doesn’t run?

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 17:37 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/6 "2019-09-13T17:37:49Z")

</div>

@afelix Hey see the code can be executed from the NodeRED editor easily. The requirement is to run when it is required. The whole system is a combination of python script's output and processing it in NodeRED.

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 17:38 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/7 "2019-09-13T17:38:21Z")

</div>

@bakman2 I will try it out.Thanks.

---

<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:** [13 September 2019 17:45 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/8 "2019-09-13T17:45:19Z")

</div>

Sounds like you can indeed call it from an exec node and process it, but have to design your flow in a way that the scripts get called at the moment(s) you want to... which there’s plenty of nodes available to take care of that depending on your needs

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 17:46 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/9 "2019-09-13T17:46:34Z")

</div>

@afelix Like which node ? can you suggest me ?

---

<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:** [13 September 2019 17:52 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/10 "2019-09-13T17:52:29Z")

</div>

That fully depends on the conditions on when to execute those. Which would be helpful if you explained those. You can use a switch node to branch a flow depending on conditions, inject/trigger/delay nodes for time based execution/repeated action. For repeated execution following a time schedule you can look at the `node-red-contrib-cron-plus` node. For repeatedly executing following script output you use the output of the `exec` node followed by processing it and more conditional logic. Possibilities are near endless.

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 17:56 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/11 "2019-09-13T17:56:31Z")

</div>

@afelix I think you didn't understand my concern fully. The python program runs whenever a user calls it. So then only the should be captured by any nodes. The "stdout" would work when it is run using editor but not from the script which is running through any python IDLE .

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 September 2019 18:18 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/12 "2019-09-13T18:18:36Z")

</div>

Just to clarify, the python script is run completely independently of Node-RED? It is not Node-RED that will be starting the script.

But when the script does run, you want its output to get to Node-RED in some form.

In which case you need to use some form of interprocess communication. A few options have already been mentioned; the python script could publish messages to a local MQTT broker (eg mosquitto) and have Node-RED subscribe to those messages. That would fully decouple the running of the script from Node-RED receiving its output. You could configure a Node-RED flow to listen on a TCP port for incoming connections - your python script could connect to that port and write its data.

There are lots of options - all depends which approach you feel comfortable with.

---

<div class="post-metadata">

**Author:** ![ALOKA1432](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@ALOKA1432](https://discourse.nodered.org/u/ALOKA1432)\
**Post date:** [13 September 2019 18:32 UTC](https://discourse.nodered.org/t/nodered-and-python/15517/13 "2019-09-13T18:32:49Z")

</div>

@knolleary Thanks. i started working on MQTT publish using paho MQTT and i got the expected results so that it kept both the things independent.
