Question

I'm currently going through Learn Python The Hard Way. I think this example might be out dated so I wanted to get feedback here on it.

I'm using Python 3.1

from sys import argv

script, first, second, third = argv

print("the script is called:", (script))
print("your first variable is:", (first))
print("your second variable is:", (second))
print("your third variable is:", (third))

I'm getting this error:

Traceback (most recent call last):
  File "/path/ch13.py", line 3, in <module>
    script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack

Any idea what's wrong?

Was it helpful?

Solution

You forgot to pass arguments to the script, e.g. foo.py bar baz quux.

enter image description here

OTHER TIPS

In order to pass arguments, you will need to run the script in this manner:

python fileName.py argument1 argument2

Depending on how many variables you have = to argv, this is how many you need to have minus the first argument (script). E.g.,

 script, first, second, third = argv

should have 3 arguments.

sys.arg is a list of command line parameters. You need to actually pass command line parameters to the script to populate this list. Do this either in your IDE's project settings or by running like this on the command line:

python script.py first second third

Note that the first argument is always the script's name (python script.py in this case). Due to your usage of unpacking, you will get a ValueError whenever you pass fewer or more than 3 parameters. You can check the number before unpacking using len(argv)-1 and presenting a suitable error if not 3.

On a related note, look at getopt if you need to do more complex argument passing.

You're trying to unpack the argv into separate values. Unpacking requires, that the exact amount of values is matched by the size of the value to unpack. Consider this:

a, b, c = [1, 2, 3]

works fine, but this:

a, b, c, d, e = [1]

will give you the same ugly error, that you just produced. Unpacking sys.argv in the way you did is especially bad, because it's user input, and you don't know, how many arguments the users of your script will supply. So you should unpack it more carefully:

if len(argv) == 5:
    script_name, a, b, c, d = argv
else:
    print "This script needs exactly four arguments, aborting"
    exit()

All you have to do is type any three things when opening the script. For example, run python (then your filename.py) 1 2 3. The "1, 2 and 3" can be replaced with any three numbers or words.

Execute the code like this:

python ch13.py first second third

In order to run this script on the command line, you need to use three arguments. You will have to type something similar to the following:

python /path/ch13.py first second third
from sys import argv
a, b, c, d = argv
print "The script is called:", a
print "Your first variable is:", b
print "Your second variable is:", c
print "Your third variable is:", d

Save this script as: s.py

Run this script from terminal as follows: enter image description here

You can do

(script, first, second, third) = argv 

and pass 3 arguments

python filename arg1 arg2 arg3

when you run it from command line.

I am using Python 3.6.0. Before i was not wrapping the argv arguments in braces. But now it works.

you can check it here

I think this example will help you. You can pass the number of arguments you want, that is, the number of parameters is variable. :D

def main(*args):
    print("Arguments Passed: ", args)

if __name__ == '__main__':
    name_Script, *script_args = sys.argv
    print("Name of the script: ", name_Script)
    main(*script_args) #Send list of arguments
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top