# Passing 2 values to python script using exec node

**URL:** <https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652>\
**Category:** General\
**Created:** [20 June 2020 08:21 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652 "2020-06-20T08:21:13Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 08:21 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/1 "2020-06-20T08:21:13Z")

</div>

Hello I have a simple questions pls,  
I've got a python script which is simply doing addition:

#!/usr/bin/python3  
import sys  
def ad(a, b):  
print(a+b)  
return a+b  
ad(int(sys.argv[1]), int(sys.argv[2]))

so in node-red i am able to call this python script by using the exec node, CMD line in exec node is: /root/add\_p.py 3 4  
so my question is: instead of simply passing constant 3 and 4, how to pass the parameter with, say, msg.payload.p1 and msg.payload.p2?  
Thank you very much for the help.

---

<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:** [20 June 2020 08:48 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/2 "2020-06-20T08:48:31Z")

</div>

If you set the exec node to "append payload as arguments", it will require your payload in the format of `3 4`. There's several ways you can get there, but my idea would be to put a change node (or function node) in front of it and create a new payload in that format. An example would be a function node with the following:

```auto
msg.payload = `${msg.payload.p1} ${msg.payload.p2}`;
return msg;

```

This syntax is the ES6 template syntax, that allows you to insert variables directly into a combined string value.

---

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 09:34 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/3 "2020-06-20T09:34:57Z")

</div>

Hi afelix, thanks for the reply. But i got error from the python script: ValueError: invalid literal for int() with base 10: 'undefined'. My inject node is: {"payload":{"p1":11,"p2":22}}.

---

<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:** [20 June 2020 09:36 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/4 "2020-06-20T09:36:57Z")

</div>

start by adding debugging to your python script. Check for the incoming arguments:

```python
print(type(sys.argv[1]), sys.argv[1])
print(type(sys.argv[2]), sys.argv[2])

```

---

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 09:57 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/5 "2020-06-20T09:57:33Z")

</div>

if I uncheck append msg.payload and CMD is: /root/add\_p.py 3 4  
I got:  
7  
\<class 'str'\> 3  
\<class 'str'\> 4

if I check append msg.payload and CMD is: /root/add\_p.py  
I got:  
Command failed: /root/add\_p.py undefined undefined  
Traceback (most recent call last):  
File "/root/add\_p.py", line 9, in   
ad(int(sys.argv[1]), int(sys.argv[2]))  
ValueError: invalid literal for int() with base 10: 'undefined'

---

<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:** [20 June 2020 09:59 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/6 "2020-06-20T09:59:11Z")

</div>

Can you add a debug node behind the node that's feeding into your exec node, and show how you've the exec node configured?

Edit: I'm seeing the same behaviour with your script in my test flow, going to take a closer look at what's happening here... I feel like I'm missing something obvious

Mine was caused by wiring it wrong. Did it again and it works just fine. Can you please upload your flow using the `</>` button? Looks like something else is going on.

---

<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:** [20 June 2020 10:11 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/7 "2020-06-20T10:11:02Z")

</div>

msg.payload needs to be a simple string containing the two values, so the payload going into the exec node must be just "3 4"  
As @afelix suggested, feed what you are sending to the exec node into a debug node and see what it shows.

---

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 10:58 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/8 "2020-06-20T10:58:41Z")

</div>

debug node after inject node shows:  
{"payload":{"p1":11,"p2":22}}  
debug node after function node shows:  
undefined undefined

function node looks like:  
msg.payload = `${msg.payload.p1} ${msg.payload.p2}`;  
return msg;

---

<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:** [20 June 2020 11:06 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/9 "2020-06-20T11:06:29Z")

</div>

Please use the `</>` button to format your code when posting some. I'm not 100% sure if your function is correct now because of how the formatting software of the forum messed with it. Can you confirm it looks like

```auto
msg.payload = `${msg.payload.p1} ${msg.payload.p2}`;
return msg;

```

And can you confirm that the debug node after inject is set to "complete message" or to "`msg.payload`". If complete message, I'm about to run out of options as the exact setup works here without an issue. If `msg.payload`, you would have payload in it twice...

> [@xiao1201](#):
>
> debug node after inject node shows:  
> {"payload":{"p1":11,"p2":22}}  
> debug node after function node shows:  
> undefined undefined

This part suggests that what goes in the function as `msg.payload.p1` and `msg.payload.p2` are already both undefined. Please check everything again to make sure it is correct.

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [20 June 2020 11:08 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/10 "2020-06-20T11:08:55Z")

</div>

what nodejs version do you use?

---

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 11:39 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/11 "2020-06-20T11:39:04Z")

</div>

hi, yes, the function code is the same as yours, i just copy it. And in inject node I chose {}JSON, the content is:

```auto
{
    "payload": {
        "p1": 11,
        "p2": 22
    }
}

```

---

<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:** [20 June 2020 11:45 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/12 "2020-06-20T11:45:09Z")

</div>

Well, there's your reason. What you inject in `{} Json` mode, is put into `msg.payload`. Try instead injecting the following object:

```auto
{
    "p1": 11,
    "p2": 22
}

```

What is happening here with your previous post is that you create a message object from your inject that looks like this:

```auto
{
    "topic": "",
    "_msgid": ".....",
    "payload": {
        "payload": {
            "p1": 11,
            "p2": 22
        }
    }

```

Take a look at the [Working with Messages](https://nodered.org/docs/user-guide/messages) part of the documentation and see if you understand the difference between the formats described here. If not, feel free to ask for more information 🙂

---

<div class="post-metadata">

**Author:** ![xiao1201](https://avatars.discourse-cdn.com/v4/letter/x/e68b1a/32.png) [@xiao1201](https://discourse.nodered.org/u/xiao1201)\
**Post date:** [20 June 2020 12:36 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/13 "2020-06-20T12:36:00Z")

</div>

yeah I made a mistake here 😂  
again, thank you so much for helping so fast 🙂

---

<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:** [4 July 2020 12:42 UTC](https://discourse.nodered.org/t/passing-2-values-to-python-script-using-exec-node/28652/14 "2020-07-04T12:42:03Z")

</div>

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