Using command line arguments in Python: Understanding sys.argv

Zack Shapiro picture Zack Shapiro · Jan 22, 2011 · Viewed 22.2k times · Source

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?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Jan 22, 2011

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

enter image description here