1
votes

I am using a Macbook Pro, running OS X Yosemite 10.10.4, and was going through the exercises in Learn Python the Hard Way. I'm running these on iPython notebooks, and their config is as below:

Python 2.7.10 |Anaconda 2.2.0 (x86_64)| (default, May 28 2015, 17:04:42) [GCC 4.2.1 (Apple Inc. build 5577)]

On Ex13, listed on http://learnpythonthehardway.org/book/ex13.html I typed and/or copied the exact code on the site but got an error.

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

On running the above code, the error message I receive is this:

ValueError Traceback (most recent call last) in () ----> 1 script, first, second, third = argv

ValueError: too many values to unpack

I tried running the code line by line, and found that the problem is when I'm assigning more than one value to argv. For example, the code below executes fully.

from sys import argv
script = argv
print "The script is called:", script

The output for the above code is:

The script is called: ['/Users/myusername/anaconda/lib/python2.7/site-packages/IPython/kernel/main.py', '-f', '/Users/myusername/.ipython/profile_default/security/kernel-261810c2-9f04-44d4-95f7-411e0db361ff.json', '--profile-dir', '/Users/myusername/.ipython/profile_default']

What could be the possible reasons for this, and how could I go about rectifying it?

Update: I tried running this via terminal as suggested, and this was the response I received. enter image description here

2
You should invoke it from the command line, exactly the way they tell you to. Don't use ipython for this exercise. - NightShadeQueen
Basically the issue is that you have 4 parameters but your script expects 3. Try putting a print(len(argv)) before your tuple unpakcing and see what the results from that are. If that's anything other than 4 you will have problems. The second code snippet will print the entire argv regardless of the number of parameters and hence not be a problem. - shuttle87
Thank you, but is this a limitation of the ipython notebooks that the code will work via Terminal but not when using the notebooks? - user2762934
Eh, it's just that you needed to pass the code arguments via the command line, and at least on my computer ipython tried to open the arguments I passed it as files, which caused it to error out :P - NightShadeQueen
Invoke your script from terminal (not python terminal) , command line terminal. (and don't write your code there) , first save it as a script and then invoke that script. - Anand S Kumar

2 Answers

1
votes

Its as the error suggests - ValueError: too many values to unpack . There are too many values on the right side, but not enough names/variables on the left side to accept them all.

In your case , your argv has 5 elements , but you are trying to store them in 4 elements. I am guessing this is something related to ipython-notebook.

You should invoke your script from command line (terminal) (just as the exercise tells you to) -

python <file> <args> 
0
votes

Just execute your script from terminal like below:

$> python ex13.py user_name