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.
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>