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

Hi all, sorry for the very long message. Following up with this post Pass input parameters to a script using pythonshell node

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": altitude, "2": direct solar, "3": diffuse solar, "4": azimuth)

They correspond to the of the payload as set in a join-node.

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"

        # 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(

exit code: 1, Traceback (most recent call last):
  File "", 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

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

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..

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, 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.

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