# Python in Node Red

**URL:** <https://discourse.nodered.org/t/python-in-node-red/29380>\
**Category:** General\
**Created:** [2 July 2020 13:05 UTC](https://discourse.nodered.org/t/python-in-node-red/29380 "2020-07-02T13:05:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [2 July 2020 13:05 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/1 "2020-07-02T13:05:35Z")

</div>

Hi All

So i want to do some python script inside node red to communicate with a S7 PLC. From Odoo btw.

I have installed the python function node but here are the following error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bba846e4243e0e13e3083e77a97ae795dfb9b00e.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a845672bd316a27fd22344592aa22d8a769f7e08.png)

I have a python script which works when i execute it with the pythonshell node but my problem is i want to edit the script with other nodes. like id numbers which it must retrieve from odoo. So if i can get that python function node to work i can write my script in that.

Or what other node is there that i can use to write the script to a file and then use the pythonshell to execute that file?

---

<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:** [2 July 2020 14:25 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/2 "2020-07-02T14:25:49Z")

</div>

You might want to review the node's issues in GitHub: [Issues · namgk/node-red-contrib-pythonshell · GitHub](https://github.com/namgk/node-red-contrib-pythonshell/issues)  
espically this one: [Not resolving module names · Issue #11 · namgk/node-red-contrib-pythonshell · GitHub](https://github.com/namgk/node-red-contrib-pythonshell/issues/11)

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [2 July 2020 14:28 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/3 "2020-07-02T14:28:17Z")

</div>

Yeah my pythonshell and exec node works fine when i run a script with it.  
My problem is when i use the [https://flows.nodered.org/node/node-red-contrib-python-function](https://flows.nodered.org/node/node-red-contrib-python-function) node that error as shown in the picture pops up.

---

<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 July 2020 14:52 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/4 "2020-07-02T14:52:24Z")

</div>

Use the exec node. You can pass parameters such as the id numbers to your script from msg.payload

---

<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:** [2 July 2020 15:12 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/5 "2020-07-02T15:12:58Z")

</div>

That's certainly an interesting issue, but the error shown in the first post, `No module named json` is striking me as especially odd as this is a builtin module in a default Python installation. It has been present since Python 2.6, if I remember correctly, which was first released in 2008 and has been EOL since 2013. I haven't even seen in it on enterprise systems in a long time. If I had to guess this is because the `python-function` node first serialises the entire msg object on Node-RED to JSON, then pushes that through an opened file descriptor into the program, so even if you don't use JSON or the JSON module in your project, if the python in your path called can't find those modules you're in trouble.

> <https://github.com/arnauorriols/node-red-contrib-python-function/blob/45d915f53ef6edc49a01cd47082289d6d2108555/lib/python-function.js#L81>

I've seen similar issues like this with python-function before. The OP is best of using either pythonshell, or an exec node with the script. I personally wouldn't attempt to use the `python-function` node, if you remember correctly (it has popped up on the forums before), it comes with this usage instruction:

> [@](#):
>
> I repeat, don't use this in production. And when you do (cause you will), please tell your manager I already told you so.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 July 2020 17:32 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/6 "2020-07-02T17:32:49Z")

</div>

> [@WImpie](#):
>
> do some python script inside node red to communicate with a S7 PLC

If you don't mind me asking, why don't you use the S7 nodes and skip python altogether?

Or is it something else you are doing in python to communicate with the PLC?

I only ask because it is quite possible what you are trying to do may be already perfectly possible in node-red and therefore avoid the need to shell python altogether.

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 06:30 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/7 "2020-07-03T06:30:49Z")

</div>

The problem is i need to send some info from [odoo.com](http://odoo.com) to the PLC and therefore i need to write a python script. Because the odoo nodes needs git installed and i cant find to do it correctly on the IOT2000...

But i found a way to send parameters into the python script which will help alot. Just need some more examples how to use that parameters.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 July 2020 07:18 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/8 "2020-07-03T07:18:38Z")

</div>

> [@WImpie](#):
>
> Because the odoo nodes needs git installed and i cant find to do it correctly on the IOT2000..

It looks like the odoo nodes depend on a repository directly from git (meaning you need git installed to install the odoo nodes)

Personally, I would raise an issue on the repository.

However, this is not a show stopper. It is possible to download the node from a pc and copy it over.

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 07:51 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/9 "2020-07-03T07:51:48Z")

</div>

Yeah i have tried it now. Dont know why i havent thought of it because that is actually how i install nodes and not through the cmd anymore..

But then this is the next error i get. Think its because i didnt install it the proper way.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d7070d4a25f1432b283440448160c4b4107bca2.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [3 July 2020 08:03 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/10 "2020-07-03T08:03:49Z")

</div>

That particular node is the dependency I was talking about.

You can simply npm install that on a different pc, then copy that too

E.g (windows)...

```auto
cmd
mkdir deps
npm init (say yes/next to everything)
npm install odoo-xmlrpc

```

Then you will find all the dependencies in `deps\node_modules`. You can copy them over and restart node-red

---

<div class="post-metadata">

**Author:** ![WImpie](https://avatars.discourse-cdn.com/v4/letter/w/eb9ed0/32.png) [@WImpie](https://discourse.nodered.org/u/WImpie)\
**Post date:** [3 July 2020 09:05 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/11 "2020-07-03T09:05:08Z")

</div>

Okay have tried it now but does not work.. but thank you. Im just going to use the pythonshell and input some parameters. I was wondering if you could help me with the following.

When there is a confirmation somewhere in this string I want to retrieve the ID of that confirmation everytime i retrieve that string shown in the pic below:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/5107fde6a4ab0a661378a9a9e52267ff4270cd15.png)

so how would i wrote a function node after the pythonshell to retrieve ID 8 because its confirmed.

---

<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:** [1 September 2020 09:05 UTC](https://discourse.nodered.org/t/python-in-node-red/29380/12 "2020-09-01T09:05:08Z")

</div>

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