0

I run this code:

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

But when I run it, I get this error:

ValueError: need more than 1 value to unpack

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
Stubborn Goat
  • 63
  • 2
  • 4
  • It seems that argv has only one value. You executed `python yourscript` instead of `python yourscript a b c` – zvone Dec 16 '15 at 22:13

2 Answers2

1

script, first, second, third = argv 'unpacks' argv (which must contain 4 items) into the corresponding variables. Apparently, you didn't pass the 3 arguments to your script.

Try this to check:

if len(argv) == 4:
    script, first, second, third = argv
else:
    print "Not enough arguments"
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • thanks for help but I get a new error :NameError: name 'script' is not defined – Stubborn Goat Dec 16 '15 at 22:19
  • @StubbornGoat: `script` would not be defined if you didn't pass 3 arguments. The execution path would have gone through the else-clause and the variables are not defined in that case. You can either move you print logic inside of the if-clause or set your variables to a default value before the if-statement. – Steven Rumbalski Dec 16 '15 at 22:25
0

This means that you are not providing enough arguments to the python script. This error means you tried to unpack more values than there were in the list. Run it like python file.py a b c.

Try this code:

if len(argv) == 4:
    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
else:
    print "Not enough arguments"
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67