0

I'm new to python and i'm just reading practicing different things and i'm trying to figure out why argv isn't working for me

from sys import argv
script, bike, car, bus = argv

print ("The script is called:"), script
print ("The first variable is:"), bike
print ("The second variable is "), car
print ("Your third variable is : "),bus

I'm getting an error of need more than 1 value to unpack

Traceback (most recent call last):
  File "ex13.py", line 6, in <module>
    script, bike, car, bus = argv
ValueError: need more than 1 value to unpack

I am running my example program from the command-line by calling:

python ex13.py
12
  • 3
    First, always put the actual exception (with traceback) in your question, don't just describe it vaguely. Commented May 18, 2015 at 0:57
  • 4
    Second, how are you running this script? If you aren't passing it any arguments (e.g., you're just doing python3 myscript.py or double-clicking myscript.py in Explorer/whatever), then argv will only have 1 element, not 4, which would give you exactly the error you're seeing. Commented May 18, 2015 at 0:57
  • 2
    That's still not the exception (with traceback), because you didn't include the traceback. Also, edit it into your question, don't post it as a comment. And finally, "I'm using pycharm to write the code and run it, and i'm also running it again in windows powershell" doesn't answer my question: show the actual Windows PowerShell line that you use to run it. If you aren't passing any arguments there, that's the problem. Commented May 18, 2015 at 1:03
  • 2
    Your print statement isn't correct either... Commented May 18, 2015 at 1:06
  • 1
    You still haven't put the information about how you run your program into your question. Since that's the actual cause of the actual problem here, that makes your question useless. Commented May 18, 2015 at 1:19

2 Answers 2

7

Your example is better written as (to cope with arbitrary usages):

from sys import argv
script, args = argv[0], argv[1:]

print("The script is called: ", script)
for i, arg in enumerate(args):
    print("Arg {0:d}: {1:s}".format(i, arg))

The reason you'd be getting an error (place show Traceback) is because you're calling your script with fewer arguments than you are trying to "unpack".

See: Python Packing and Unpacking and Tuples and Sequences where it says:

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

To demonstrate what's going on with your example adn eht error you get back:

>>> argv = ["./foo.py", "1"]
>>> script, a = argv
>>> script
'./foo.py'
>>> a
'1'
>>> script, a, b = argv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

The error should be obvious here; You are trying to unpack more values than you have!

To fix your example I'd do this:

from sys import argv

if len(argv) < 4:
    print ("usage: {0:s} <bike> <car> <bus>".format(script))
    raise SystemExit(-1)

script, bike, car, bus = argv

print ("The script is called:", script)
print ("The first variable is:", bike)
print ("The second variable is ", car)
print ("Your third variable is : ", bus)

Update: I just noticed this; but all your print()(s) are wrong. You need to either use str.format() or put the argument inside the print() function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks the book i'm using to learn python didn't really explain what what python packing was in much detail, which explains why i was so far from the correct concept.
Then I'd say that's not a very good book to learn from. Personally I find the Python Tutorial invaluable.
3

Pycharm is an ide and i just click run and it runs the program, but in powershell i type in python ex13.py and that runs the program

OK, then you aren't passing any arguments. So, what were you expecting to find as as the first, second, and third arguments? PowerShell isn't going to guess what bike, car, and bus you wanted to pass the program, any more than it's going to go out and buy you a bike, car, and bus. So, if you want to run this program with arguments representing your bike, car, and bus, you have to actually do that:

python ex13.py CR325 Elise VW

Then your script will output those arguments.

Well, actually, it may not, because your print calls are wrong. If this is Python 2.7, those parentheses don't do anything, so you'll see:

The script is called: ex13.py
The first variable is: CR325
The second variable is  Elise
The third variable is :  VW

If it's Python 3.x, the parentheses wrap the arguments to the print, just like any other function, so the , script and so forth aren't part of the print, so you'll just see:

The script is called: 
The first variable is: 
The second variable is 
The third variable is : 

2 Comments

Great explanation on all the things that could go wrong with the initila implementation :) +1
Indeed great explanation, i'm new to python so i didn't know about the parentheses and print call variations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.