# Python code in node-red

**URL:** <https://discourse.nodered.org/t/python-code-in-node-red/35106>\
**Category:** General\
**Created:** [31 October 2020 04:54 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106 "2020-10-31T04:54:51Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [31 October 2020 04:54 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/1 "2020-10-31T04:54:51Z")

</div>

Dear All,

I am writing python code which uses machine learning algorithm like LSTM to forecast the future stock data. The entire code is developed in Jupyter notebook. So the extension of file in ipynb.

Is there any node or a way I can run this code in node-red.

Thanks & Regards  
Varun

---

<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:** [31 October 2020 08:21 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/2 "2020-10-31T08:21:38Z")

</div>

Hi Varun,  
Have you searched the forum? I don't use python, but the forum is full of python related stuff...  
E.g. [here](https://discourse.nodered.org/t/python-function-node-unicodedecodeerror/34986/3).

---

<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:** [31 October 2020 08:55 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/3 "2020-10-31T08:55:22Z")

</div>

The easier (imho) way would be to either use an exec node to call your program and get the result back, or a daemon node if its a long running app that needs to be fed data.

---

<div class="post-metadata">

**Author:** ![Huda](https://avatars.discourse-cdn.com/v4/letter/h/7cd45c/32.png) [@Huda](https://discourse.nodered.org/u/Huda)\
**Post date:** [31 October 2020 09:06 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/4 "2020-10-31T09:06:24Z")

</div>

Hello, one thing you can try is the pythonshell node found here:

> **[node-red-contrib-pythonshell](https://flows.nodered.org/node/node-red-contrib-pythonshell)**
>
> nodes used to interact with python processes

You just need to give it the path to your file in the Py file field (for example; /Users/namtrang/main.py ). I am not sure if it runs ipynb extension, though. However, from your jupyter notebook, you can save as .py instead of .ipynb and the code works the same from what I have tried before, so it will hopefully do what you need.

---

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [1 November 2020 23:35 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/5 "2020-11-01T23:35:45Z")

</div>

Thanks for your response,

When I use below code in python shell in windows machine I am getting an error exit code: -4058.  
location of python in my windows looks to be correct as where is showing below path in windows

C:\Users\CQUGSR\>where python  
C:\Python38\python.exe  
C:\Users\CQUGSR\AppData\Local\Microsoft\WindowsApps\python.exe

```auto
[{"id":"b1415d99.fb7fc","type":"tab","label":"Flow 1"},{"id":"6e284a45.462364","type":"pythonshell in","z":"b1415d99.fb7fc","name":"","pyfile":"C:\\Varun\\Test\\test.py","virtualenv":"C:\\Python38\\python.exe","continuous":false,"stdInData":false,"x":341.5,"y":154,"wires":[["da45a630.24ff18"]]},{"id":"a39228fa.530ee8","type":"inject","z":"b1415d99.fb7fc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":140.5,"y":76,"wires":[["6e284a45.462364"]]},{"id":"da45a630.24ff18","type":"debug","z":"b1415d99.fb7fc","name":"","active":true,"console":"false","complete":"false","x":537.5,"y":233,"wires":[]}]

```

Thanks & Regards  
Varun

---

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [2 November 2020 00:31 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/6 "2020-11-02T00:31:30Z")

</div>

Now I am able to run the python scripts using exec node.

In my script I am using pandas. It showing an error saying pandas not found. But I could able to execute and test my complete logic in Juypyter notebook.

Can any one suggest me how to include Pandas and test in node red

Error message:  
Command failed: python C:\Varun\Test\LSTM\_FC-s30-test.py 1604277501039Traceback (most recent call last): File "C:\Varun\Test\LSTM\_FC-s30-test.py", line 15, in import pandas as pdModuleNotFoundError: No module named 'pandas'

Thanks & Regards  
Varun

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2020 09:28 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/7 "2020-11-02T09:28:21Z")

</div>

Does that command run if you run it from the command line?

---

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [2 November 2020 09:59 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/8 "2020-11-02T09:59:33Z")

</div>

In command line also it is giving warning but execution completes. Looks like in node-red it is stooping the execution

I am not sure it is stopping because of "newline" option not supported in python 2.7.17. I have an constraint of not upgrading to latest version of python too.

data = [['S10']]

# opening the csv file in 'w+' mode

file = open('S10.csv', 'w+', newline ='')  
write = csv.writer(file)  
write.writerows(data)

# writing the data into the file

with file:  
write = csv.writer(file)  
write.writerows(data\_FC)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2020 10:07 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/9 "2020-11-02T10:07:46Z")

</div>

> [@varunyc](#):
>
> In command line also it is giving warning

What warning?

---

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [2 November 2020 10:40 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/10 "2020-11-02T10:40:30Z")

</div>

This is the warning I am getting

Train on 1613 samples, validate on 823 samples  
WARNING:tensorflow:From /home/ubuntu/.local/lib/python2.7/site-packages/tensorfl ow/python/ops/math\_grad.py:1250: where (from tensorflow.python.ops.array\_ops) is deprecated and will be removed in a future version.  
Instructions for updating:  
Use tf.where in 2.0, which has the same broadcast rule as np.where  
Epoch 1/100  
2020-11-02 16:42:54.665935: I tensorflow/core/platform/cpu\_feature\_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA  
2020-11-02 16:42:54.673302: I tensorflow/core/platform/profile\_utils/cpu\_utils.c c:94] CPU Frequency: 2299995000 Hz  
2020-11-02 16:42:54.673586: I tensorflow/compiler/xla/service/service.cc:168] XL A service 0x561086aa7ef0 executing computations on platform Host. Devices:  
2020-11-02 16:42:54.673631: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): ,   
2020-11-02 16:42:54.822872: W tensorflow/compiler/jit/mark\_for\_compilation\_pass. cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF\_XLA \_FLAGS=--tf\_xla\_cpu\_global\_jit was not set. If you want XLA:CPU, either set tha t envvar, or use experimental\_jit\_scope to enable XLA:CPU. To confirm that XLA is active, pass --vmodule=xla\_compilation\_cache=1 (as a proper command-line flag , not via TF\_XLA\_FLAGS) or set the envvar XLA\_FLAGS=--xla\_hlo\_profile.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2020 10:58 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/11 "2020-11-02T10:58:25Z")

</div>

I note that you have opened another thread with a similar problem. Does that mean that this one is fixed?

---

<div class="post-metadata">

**Author:** ![varunyc](https://avatars.discourse-cdn.com/v4/letter/v/8dc957/32.png) [@varunyc](https://discourse.nodered.org/u/varunyc)\
**Post date:** [2 November 2020 10:59 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/12 "2020-11-02T10:59:20Z")

</div>

Yes You are right..Using execute node solved the problem

---

<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:** [16 November 2020 10:59 UTC](https://discourse.nodered.org/t/python-code-in-node-red/35106/13 "2020-11-16T10:59:36Z")

</div>

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