#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Location
    Sydney, Australia
    Posts
    63
    Rep Power
    3

    ValueError: Need more than 1 value to unpack.


    ValueError: Need more than 1 value to unpack.

    So this is my error, I'm following learn python the hard way exercise 13. Made this thread on Beginners but it was taking a while for a reply.

    Anyway, what I had to do was type "python ex13.py first 2nd 3rd"into the python shell i think to run the program, but I get the error 'ex13 syntax error'. That's my first problem...(I've never run .py from the python shell before, usually just press f5)

    So I did some searching for solutions and I came across "execfile("ex13.py")", tried that but I get ValueError: Need more than 1 value to unpack.

    I tried it again by typing import sys, sys.argv with the arguments, followed by execfile but I get the same error but now it's saying 3 value's.

    There was another solution on the same thread, which I haven't tried yet because it looks strange and I'm not sure if it's an optimal solution. (I don't know if it would cause problems down the track in larger programs) it is this function:

    Code:
    import subprocess
    subprocess.call(["D:/Python24/Python.exe", "Skripta.py", "arg1", "arg2"])

    Here is my code:

    Code:
    import sys 
    from sys import argv
    
    sys.argv = ['first', '2nd', 'here']
    
    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
    Thanks for any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    438
    Rep Power
    67
    Originally Posted by breadbox
    Anyway, what I had to do was type "python ex13.py first 2nd 3rd"into the python shell
    Into the PYTHON shell?! That should be typed in the OSís command line shell.

    So I did some searching for solutions and I came across "execfile("ex13.py")", tried that but I get ValueError: Need more than 1 value to unpack.
    execfile() takes only one obligatory argument, so the syntax error results from something inside ex13.py.

    Code:
    sys.argv = ['first', '2nd', 'here']
    Why are you assigning to sys.argv? While not illegal, thatís usually not what you want to do, since sys.argv holds the command line given to your Python script.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    You're beginning python and you're having to import subprocess?

    STOP! This path is not helpful. Seems to me you'd be better off trying experiments in the interpreter. For instances:

    >>> 3*8
    24
    >>> 'hello world'.upper()
    'HELLO WORLD'
    >>> L = [1,2,3]
    >>> L[1]
    2
    >>> L[1:]
    [2, 3]


    If you want to learn about multiple assignment try
    Code:
    >>> a,b = 1,2
    >>> a
    1
    >>> b
    2
    >>> a,b = [1,2,3]                       # this is the error
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: too many values to unpack
    But mercy me! Why couple that with trying to understand command line arguments at the same time? Back up man.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Location
    Sydney, Australia
    Posts
    63
    Rep Power
    3
    Thanks for the reply guys.

    So is this maybe too advanced for me? I was just following the book, and ran into the problems...I tried using those functions because it's all I could find online.

    Can't I run it from the python command line? I type 'python ex13.py first 2nd 3rd' but I still get syntax error for the script. I removed sys.argv, I don't think I have any syntax errors in the file...

    I still need to figure out how to use the OS command line so if that's the issue let me know. I'm moving tomorrow and have to organize so I'll try and understand this more then. Thanks for any advice/solutions you can provide until then...
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481

    Try this program


    This program will run the same in python2 and in python3. It also works and demonstrates exercise 13 in a failsafe manner.

    Open a command window, and try the commands super oscar indicated (assuming python is where you say)

    D:/Python24/Python.exe Skripta.py

    D:/Python24/Python.exe Skripta.py FirstArgument

    D:/Python24/Python.exe Skripta.py 1st 2nd

    Code:
    import sys
    
    print('sys.argv is '+str(sys.argv))
    print('')
    print('')
    
    (a,b,) = (sys.argv+['python command line had no arguments'])[:2]
    
    print('program name: '+a)
    print('the argument (or not): '+b)
    
    input('press the enter button on your keyboard to continue')
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Location
    Sydney, Australia
    Posts
    63
    Rep Power
    3
    Originally Posted by b49P23TIvg
    This program will run the same in python2 and in python3. It also works and demonstrates exercise 13 in a failsafe manner.

    Open a command window, and try the commands super oscar indicated (assuming python is where you say)

    D:/Python24/Python.exe Skripta.py

    D:/Python24/Python.exe Skripta.py FirstArgument

    D:/Python24/Python.exe Skripta.py 1st 2nd

    Code:
    import sys
    
    print('sys.argv is '+str(sys.argv))
    print('')
    print('')
    
    (a,b,) = (sys.argv+['python command line had no arguments'])[:2]
    
    print('program name: '+a)
    print('the argument (or not): '+b)
    
    input('press the enter button on your keyboard to continue')
    Tried it, and it works! Thank you. Now I just need to try and understand it.

    With this line

    Code:
    (a,b,) = (sys.argv+['python command line had no arguments'])[:2]
    What does [:2] do? I don't think I've used it before.

    Thank you for the help.

    Edit: Does [:2] show the string when sys.argv has no values somehow?

    Edit2: Something else that's bugging me is I can't open a script from cmd by typing "C:\Python27\python.exe Skripta.py" (that's correct directory)...I have to type C:\Python27\python to run python and then execfile("C:\Python27\Skripta.py")

    Which isn't great because I can't type in arguments.
    I entered the arguments in cmd using sys.argv = [] which worked, but every time after that I get a 'name 'sys' is not defined'. So now I can't give the script any cmd arguments :S
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,895
    Rep Power
    481
    I was expert with dos 2.1. Now I try to avoid Microsoft.
    Code:
    >>> a = [1,'two','3rd item at index 2','d','e','f']  # a list
    >>> a[0]   # the item at first index (index origin 0)
    1
    >>> a[2]
    '3rd item at index 2'
    >>> a[:2]  # a list of the first 2 items
    [1, 'two']
    >>> a[1::2] # list of the items at odd indexes
    ['two', 'd', 'f']
    >>>
    At startup python guarantees sys.argv is a list of minimum length 1. To display the first two items of sys.argv , I first extended sys.argv by one item to guarantee that it will have two items. Four ways to lengthen a list:
    append, extend, +, and insertion.

    Code:
    >>> a = [1,'two','3rd item at index 2','d','e','f']  # a list
    >>> a[0]   # the item at first index (index origin 0)
    1
    >>> a[2]
    '3rd item at index 2'
    >>> a[:2]  # a list of the first 2 items
    [1, 'two']
    >>> a[1::2] # list of the items at odd indexes
    ['two', 'd', 'f']
    >>> a + ['another list']
    [1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'another list']
    >>> ['another list'] + a # concatenate (join) two lists
    ['another list', 1, 'two', '3rd item at index 2', 'd', 'e', 'f']
    >>> a[:0]=['at front']
    >>> a
    ['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f']
    >>> a[len(a):]=['a','list','at','end']
    >>> a
    ['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end']
    >>> a.extend('items')
    >>> a
    ['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 's']
    >>> a.append('item')
    >>> a
    ['at front', 1, 'two', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 'item']
    >>> a[3:3]=['insertion']
    >>> a
    ['at front', 1, 'two', 'insertion', '3rd item at index 2', 'd', 'e', 'f', 'a', 'list', 'at', 'end', 'i', 't', 'e', 'm', 'item']
    >>>
    Anyway, so I made sure there were at least two items in sys.argv and then displayed them, with sys.argv[:2]

    Comments on this post

    • breadbox agrees
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Location
    Sydney, Australia
    Posts
    63
    Rep Power
    3
    Thank you immensely b49P23TIvg! Really helped. I couldn't figure out how to fix the 'name 'Skripta' is not defined' error when running the script from cmd...But I'm using Pythonwin now which lets you type the arguments in when you run from the IDE. Is there any drawbacks to entering arguments this way?

    Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo