Passing multiple arguments to Python script using sys.argv[1] and numpy array. IndexError: list index out of range

1.8k Views Asked by At

I'm working in a small code to receive multiple arguments from an MQTT server and use them to predict another value. I'm showing a simplified code here just to get some help. To pass the arguments to the script for executing the prediction, first part is the creation of a numpy array, then pass the arguments to the script using sys.argv[], then the indexing to position the incoming values.

import numpy as np
import sys

    # creating empty numpy array for feature values
X = np.empty(2).reshape(1, 2)

    #storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[2]

    #displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
   
    # set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[2]

print(X)

However, I have an issue with the second argument as I get an error:

exit code: 1, Traceback (most recent call last): File "numpy-array.py", line 10, in azimuth_cos=sys.argv[2] IndexError: list index out of range

The only way to avoid that error is if I set both arguments to: sys.arg[1]

   #storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[1]

   #displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
   
   # set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[1]

print(X)

Then I get two consecutive outputs:

azimuth_sin : -0.9152180545267792 azimuth_cos : -0.9152180545267792 Number of arguments : 2 [[-0.91521805 -0.91521805]]

and:

azimuth_sin : 0.40295894662883136 azimuth_cos : 0.40295894662883136 Number of arguments : 2 [[0.40295895 0.40295895]]

which are actually the values of the two arguments printed, but repeated twice: sin = -0.9152180545267792 and cos = 0.40295894662883136

If I put the arguments in one line:

   #storing the arguments
azimuth_sin, azimuth_cos = sys.argv[1:2] 

The error is:

exit code: 1, Traceback (most recent call last): File "numpy-array-t1.py", line 10, in azimuth_sin, azimuth_cos = sys.argv[1:2] ValueError: not enough values to unpack (expected 2, got 1)

I've tried many ways to fix this without success, I'd appreciate any help or suggestions. Thank you in advance.

2

There are 2 best solutions below

1
ComputerKarate On

Start with something simple to validate the data you are receiving.

Do something like:

import sys  
  
# Verify first so you don't get an error  
# This check verifies we have at least two parameters
if 1 < len(sys.argv):
    if sys.argv[1]:
        var_argv1 = sys.argv[1]
        print("var_argv1 type: %s, length: %s" % (type(var_argv1), len(var_argv1)))

        # It is pointless to continue if argv[1] has no data
        if sys.argv[2]:
            var_argv2 = sys.argv[2]
            print("var_argv2 type: %s, length: %s" % (type(var_argv2), len(var_argv2)))
        else:
            print("sys.argv[2] has no data")
    else:
        print("sys.argv[1] has no data")

It may be that you are trying to process a numpy object on the command line

Note:
Do you have access to the MQTT server?
It might be easier to pick a channel (topic) to use for this data transfer.
You could get the MQTT server to publish this data on a channel and have this script subscribe to that channel.
Then you could make sending information as easy as a function call on your MQTT system.

1
hpaulj On

In Linux terminal window, I have a simple script that just displays sys.argv:

1619:~$ cd mypy
1619:~/mypy$ cat echo.py
import sys
print(sys.argv)

When I call it thus:

1619:~/mypy$ python3 echo.py 1.23 3.112 foo bar
['echo.py', '1.23', '3.112', 'foo', 'bar']

See that sys.argv is list of 5 strings, which come from the commandline.

If you are calling your script from a shell or windows command window, you should be able to enter and see multiple strings.

But people have problems using sys.argv (and argparse) when running the script from something like pydev or as a Jupyter notebook. I don't know anything about the MQTT server so can't help with providing even one command line argument. As I demonstrate, sys.argv is primarily intended as a way of providing startup values and options when running a script from the operating system.