# From Python script to Node RED

**URL:** <https://discourse.nodered.org/t/from-python-script-to-node-red/1352>\
**Category:** General\
**Created:** [5 July 2018 10:56 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352 "2018-07-05T10:56:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhoemio](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bhoemio](https://discourse.nodered.org/u/Bhoemio)\
**Post date:** [5 July 2018 10:56 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/1 "2018-07-05T10:56:43Z")

</div>

Hello there,

Is there a way to execute a while python script in my PC and display this informations one by one in Node-RED?

My Python script is:

count = 0  
while count \< 10:  
print(‘Hello’)  
count += 1  
But what displays in the screen is:

{“topic”:"","\_msgid":“6287885c.42fc38”,“payload”:“Hello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\n”,“rc”:{“code”:0}}

How can I displays these information in separate payloads without having to run the script over and over?

---

<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:** [5 July 2018 10:59 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/2 "2018-07-05T10:59:54Z")

</div>

If that’s your real script why not recreate it in javascript?

If you really want use python how are you executing the python script? are you using the contrib-node or are you using the exec node?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [5 July 2018 11:02 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/3 "2018-07-05T11:02:22Z")

</div>

If the only point is to separate the payloads why not to use the standard split node from the pallete?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1a3d5b91abbf6efc8ef57c6d5b09d6436ed0797a.png)

---

<div class="post-metadata">

**Author:** ![Bhoemio](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bhoemio](https://discourse.nodered.org/u/Bhoemio)\
**Post date:** [5 July 2018 11:03 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/4 "2018-07-05T11:03:55Z")

</div>

Thank you for the reply!  
I am using exec node.  
This script is an exemple for a bigger one, and unfortunately I can’t use a python3 function node.

---

<div class="post-metadata">

**Author:** ![Bhoemio](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bhoemio](https://discourse.nodered.org/u/Bhoemio)\
**Post date:** [5 July 2018 11:14 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/5 "2018-07-05T11:14:12Z")

</div>

Thank you for the reply.  
I have another python script that has not built-in libraries, my idea is to use Exec Node to run this script and the script will run a while for an undetermined time.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 July 2018 12:02 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/6 "2018-07-05T12:02:51Z")

</div>

add a delay in the python script and set the exec node output to:  
while the command is running - spawn mode

```auto
#!/user/bin/python
import time
count = 0
while count < 10:
	print('Hello')
	count += 1
	time.sleep(1)

```

---

<div class="post-metadata">

**Author:** ![Bhoemio](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bhoemio](https://discourse.nodered.org/u/Bhoemio)\
**Post date:** [5 July 2018 12:43 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/7 "2018-07-05T12:43:56Z")

</div>

oh my god, thank you for the reply!  
The change exec node output solve my problem o/

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [5 January 2022 16:21 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/8 "2022-01-05T16:21:39Z")

</div>

Hi, I'm trying to do something similar, but I need to have a message for each print within the loop.  
Now I can only get one message at the and of the script. This since I have a never ending while loop.  
Of course I set the node in spawn mode  
Thanks

---

<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:** [5 January 2022 16:37 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/9 "2022-01-05T16:37:09Z")

</div>

is this using a python script ? If so did you use the -u option as per the info ?

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [5 January 2022 16:39 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/10 "2022-01-05T16:39:00Z")

</div>

Done 🙂  
just putting "python -u patch" instead of "python patch" in the exec node command  
thanks a lot

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 January 2022 16:43 UTC](https://discourse.nodered.org/t/from-python-script-to-node-red/1352/11 "2022-01-05T16:43:30Z")

</div>


