# Pass input parameters to a phytonshell node using sys.argv: IndexError: list index out of range

**URL:** <https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709>\
**Category:** General\
**Created:** [15 January 2022 20:34 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709 "2022-01-15T20:34:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miinch](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@miinch](https://discourse.nodered.org/u/miinch)\
**Post date:** [15 January 2022 20:34 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709/1 "2022-01-15T20:34:48Z")

</div>

Hi all, sorry for the very long message. Following up with this post [Pass input parameters to a script using pythonshell node](https://discourse.nodered.org/t/pass-input-parameters-to-a-script-using-pythonshell-node/56432)

 ![Q2-join to write file](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1f9fe4f7b07b4339f6448c976fcce43febf62a63.png)

I'm trying to pass 4 arguments (altitude, azimuth, solar radiation), from an MQTT server to a script using the **pythonshell node**. I first passed the incoming values to a file that has this basic format:

{" **1**":10.9," **2**":121.0," **3**":52.7," **4**":238.09}

("1": altitude, "2": direct solar, "3": diffuse solar, "4": azimuth)

They correspond to the _msg.parts.index_ of the payload as set in a **join-node**.

 ![Q2-join parts index](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/618c7c727b8b57506469d3ed4b11dbdb3372a2bc.png)

The script is copied below, there is a preceeding step to create a numpy array, and to calculate the azimuth, I included it so its easier to understand the script. I edited the script to assign index values to the arguments using **sys.argv[1]** to the " **main**"

```auto
        # numpy array for feature values
        X = np.empty(5).reshape(1, 5)
        
		# azimuth sin and azimuth cos
        max_a = 123.456
        azimuth_sin = np.sin((2*np.pi*azimuth/max_a))
        azimuth_cos = np.cos((2*np.pi*azimuth/max_a))
		
		# set vector values
        X[:,0] = altitude
        X[:,1] = direct_solar
        X[:,2] = diffuse_solar
        X[:,3] = azimuth_sin
        X[:,4] = azimuth_cos

        new = self.model_new.predict(X)

        return new

if __name__ == " __main__":

    predict = Mymodel()
    new = predict.return_predictions(
        altitude=sys.argv[1], 
        direct_solar=sys.argv[2], 
        diffuse_solar=sys.argv[3], 
        azimuth=sys.argv[4], 
         
        print(new)

```

```auto
ERROR:

exit code: 1, Traceback (most recent call last):
  File "test-prediction.py", line 46, in <module>
    diffuse solar=sys.argv[2], 
IndexError: list index out of range

```

I'd to ask if this is the best approach, or if there is other way you can suggest to pass the arguments. Also, it could be that the format given in the output file is also causing issues due to the special characters (" : ,), or if it would be better to give the values in a single column.

I'd appreciate your help, thank you

---

<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:** [15 January 2022 22:07 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709/2 "2022-01-15T22:07:56Z")

</div>

> [@miinch](#):
>
> I'd to ask if this is the best approach, or if there is other way you can suggest to pass the arguments.

Is the python so complicated that you couldn't simply do this calculation in JavaScript in a function node? It would greatly simplify things.

---

<div class="post-metadata">

**Author:** ![miinch](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@miinch](https://discourse.nodered.org/u/miinch)\
**Post date:** [16 January 2022 10:39 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709/3 "2022-01-16T10:39:34Z")

</div>

Hi, thank you for your reply. I've been actually thinking about splitting the code in different parts, but haven't try yet, I'll look at it. thanks..

---

<div class="post-metadata">

**Author:** ![murphybeck](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@murphybeck](https://discourse.nodered.org/u/murphybeck)\
**Post date:** [17 January 2022 08:52 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709/4 "2022-01-17T08:52:30Z")

</div>

> [@miinch](#):
>
> `IndexError: list index out of range`

The way Python indexing works is that it starts at 0, so the first number of your list would be [0]. Index Error: List index out of range means you are trying to get an item in a list that doesn't exist. It means that you are referring to n-th element of the [python list](http://net-informations.com/python/ds/lists.htm), while the length of the list is smaller than n. whenever you get this type of error please cross check with items that comes between/middle in range, and insure that their index is not last if you get output then you have made perfect error that mentioned.

An index in Python refers to a position within an ordered list . To retrieve an element of the list, you use the index operator `([])` . Using indexing you can easily get any element by its position.

---

<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:** [18 March 2022 08:52 UTC](https://discourse.nodered.org/t/pass-input-parameters-to-a-phytonshell-node-using-sys-argv-indexerror-list-index-out-of-range/56709/5 "2022-03-18T08:52:57Z")

</div>

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