10

Disclaimer: I have read the thread located at Python Error: "ValueError: need more than 1 value to unpack", and none of the answers explained how to actually get the code to run.

I am reading Learn Python the Hard Way, and I am on Exercise 13.

Here is the code I am attempting to run in the IDLE thing (don't know what it is really called) for Python 2.7:

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

When run, I get the error:

"Traceback (most recent call last): File "C:\Python\LPTHW\examples_LPTHW\ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack"

I have tried running the program through the Windows command prompt, to no avail. I have also run the program by typing: "python ex13.py first 2nd 3rd", in both the IDLE and the command prompt, also got the same error.

How do I get this code to run, and what the heck am I doing wrong?

EDIT: @John Machin, I made a program exactly like the one you posted, saved it as a .py, then went into the correct directory where my file was saved, ran the program using "python yourcode.py B C D" and I received the following message "python is not recognized as an internal or external command..." When I run the program by just typing it's name (which works for all other programs besides this one) I get an error saying "SyntaxError: invalid syntax on line 2: print len(argv), repr(argv)".

EDIT 2 (With the new code): @John Machin, I copied your program word for word into gedit and made the .py file as usual. I opened my terminal (command prompt) and typed:

python ex13c.py B C D

And received:

'python' is not recognized as an internal or external command, operable program or batch file."

Then I typed:

ex13c.py

And received:

3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)]
1 ['C:\\Python\\LPTHW\\examples_LPTHW\\ex13c.py']
Traceback (most recent call last):
  File "C:\Python\LPTHW\examples_LPTHW\ex13c.py". line 4, in (module)
    a, b, c, d = argv
ValueError: need more than 1 value to unpack

Keep in mind I am running W7 64bit, and that Zed is teaching in 2.X and I have been using 2.7 the entire book so far. Every other example has worked. Right before I started Example 11, I installed 3.1 for a different book I am about to start reading. Every time I use the IDLE included with 2.7. Perhaps it is possible I am somehow inadvertently using 3.1 while trying to code in 2.7?

Thanks for bearing with me.

Community
  • 1
  • 1
rayne117
  • 671
  • 2
  • 6
  • 7

19 Answers19

3

The example works. It seems like you aren't running it correctly.

Firstly, IDLE is a somewhat special environment that I never saw the point of, just run it with the normal Python interpreter.

Secondly, specify the whole path to your python interpreter. Instead of

 python ex13c.py B C D

You on Windows need to do something like this:

 "C:\Program Filed\Python-2.7.1\Python.exe" ex13c.py B C D

Reading exercise 13 I also see that Zed calls modules "features" up until chapter 13. There is no point in doing that, that's just confusing. He also in some chapter tells people to memorize the output of logical tables, which is nonsense. You don't need to memorize them, you need to understand them.

Each time this book pops up in questions here I get less impressed with it. I'm sure there must be a better tutorial. Perhaps Dive into Python?

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
2
from sys import argv
a, b, c, d = argv
print "The script is called:", a
print "Your first variable is:", b
print "Your second variable is:", c
print "Your third variable is:", d

Save this script as: s.py

Run this script from terminal as follows: enter image description here

Sayali Sonawane
  • 12,289
  • 5
  • 46
  • 47
2

Try tunning it like "python test.py foo bar baz"

If it works like that but fails if you call it without specifying the interpreter look here: http://bugs.python.org/issue7936

bdew
  • 1,330
  • 9
  • 22
2

Boil your code down to these 4 lines:

from sys import argv
print len(argv), repr(argv)
a, b, c, d = argv
print a, b, c, d

and run it in a Command Prompt window by typing

python yourcode.py B C D

Then copy/paste the exact result into an edit of your question. Ensure that you show the ^ produced with any syntax error. Also show the exact code that you ran: type yourcode.py

Edit Use this instead:

from sys import argv, version
print(version)
print(len(argv), repr(argv))
a, b, c, d = argv
print(a, b, c, d)

Edit 2

FIRST TRIAL: You did: python ex13c.py B C D and got nowhere. Now try: c:\PYTHON\python ex13c.py B C D (I'm assuming from previous evidence that that's where some version of Python is located)

SECOND TRIAL: You did: ex13c.py with NO ARGUMENTS (why?) and the result was: (1) It appears that .py is associated with a 3.1.3 Python executable somewhere, because it was the last version installed (2) Because you supplied no arguments, len(argv) is 1, and argv contains only the path to your script.

SUGGESTIONS:

You want to have 2.7 and 3.1 on the same machine. This is quite possible (I have 3.1 and 2.1 to 2.7 inclusive because I support software that runs on those versions, and 1.5.2 for nostalgia) with only a mild amount of care. The main things that you need to do are (1) Install Python X.Y in a directory c:\pythonXY (2) run scripts from the commandline like this: \python27\python myscript.py arg1 arg2 etc (3) Don't (as you have done at the moment) put your own scripts and data in a software directory like c:\Python31.

So: A. Set up new script and data directories for "book1" and "book2". Copy all existing code and data to those new directories. Take a backup. B. Un-install all versions of Python C. Install 2.7 and 3.1 to their respective directories. D. Check that you can run your scripts from their new locations.

John Machin
  • 81,303
  • 11
  • 141
  • 189
  • 1
    seems that the OP problem is he does not knwo how to run anything from a terminal. Yet another GUI guy. – kriss Dec 22 '10 at 12:00
1

I'm reading that same book too! "Learn python the hard way" by Zed.A.Shaw, the second edition. I stuck in the exactly same exercise! It took me days and days to find out just why I couldn't get the program run. I had googled on this alot and was rather surprised to see that I was not the only one to have this problem.

BUT NOW I KNOW HOW TO FIX IT AND IT NOW WORKS ...LIKE A CHARM

First, I read Kriss's answered to "what the terminal is?" (Actually, I didn't know what that was too. I've been coding everything in LPTHD in a text editor-which can be run by choosing File --> New --> New Window in "IDLE(pythonGUI)") I then figured out (thanks to Kriss) that the terminal should be the original command prompt (not after you have run python on it)

After that I typed python D:/abc/ex13.py 1st second third in the command prompt.(I stored the file that I coded using the text editor provided by IDLE in th D:/abc). However, it did not work, saying that there was no such file/ directories.

Curious enough, I take a look at my file and noticed that there wasn't the EXTENSION at all (actually, the type of the file was "file"). So I decided to add ".py" in the file name and ,obviously, changed it into a python file.

I tried again, typing the same command line above in the command prompt and the miracle just happened, it worked!!!

I don't know if any of you might need this or if this maybe the answer for you, but since I've been through such hard time with this problem that it almost made me decided to change the book (cause the later chapter of LPTHD is based on this one), I decided to post this here in hope that it may help someone.

ttriet204
  • 353
  • 1
  • 5
  • 13
1

This one took me some time to figure out too, so let me see if I can't help the next guy who stumbles across this page.

If your specific error message is: "ValueError: need more than 1 value to unpack" and you've been able to run through the examples without issue so far, you probably have your python installed correctly, you just don't understand HOW to run this specific example, or WHY it works the way it does.

Without over-complicating what you have yet to learn, sys is a library and argv is a function of the sys library. It might be easier to think of the sys library like a book, and the argv function like a chapter inside that book. These libraries and functions are things that other people have already created and you are 'calling' or 'importing' them in to your program. This saves you the time from re-writing them yourself. Line one basically says: From the sys book, use chapter argv.

from sys import argv

Before we go any further, we need to have an understanding of what the argv function is. Later you'll learn what an array is, but for now think of argv like a shopping list. A shopping list can have 1 item or 20, depending on what you need, but before you can use it you have to decide two things:

  • How many items do I want to shop for?
  • What are those items?

When you first import argv its basically like a blank piece of paper, with no information on it. You can test this by running the following from python directly inside the command line. (Open the command prompt or PowerShell & type python. Then enter the following 2 lines)

>>> from sys import argv 
>>> print argv

The result is an empty array, or on our example a shopping list.

['']

To start assigning values, we'll take the same exact two lines of code, but instead of entering them in python, we will save them as a .py file (I saved mine as ex_13.py). Then, we'll run this code from the command line like we have all of our other examples. Note if your still inside python type: exit() to return to the command line

Now, when we run our program using:

python ex_13.py 

The result is:

['ex_13.py']

As you can see, the argv function is no longer empty, but instead it holds the name of your file! But, where did it GET that value? and how do we assign more than one of them?

When we wrote our ex_13.py file, the argv function we imported expects to have at least one value assigned to it. To assign that value, we then ran ex_13.py, and the function used whatever the name of the file we typed in and assigned it to that first value. To add more items to our list, we would just continue these two separate steps! We can show this by modifying our ex_13.py like this:

from sys import argv
a, b = argv # Here we tell argv to expect 2 values to be passed to it.
print argv

Now. If you try to run this script from the command line using:

python ex_13.py

You probably got the following error message:

ValueError: need more than 1 value to unpack

The reason is because we specified in text of our file that we wanted two arguments (values / items) , but when we ran it we only provided one. To successfully execute this script, try typing this instead:

python ex_13.py Peaches!

This time, the result is an array, with both arguments passed.

['ex_13.py','Peaches!']

Now when we go back to the original lesson it becomes much easier to understand what it is we're doing and how to get the program to execute without error. Update your ex_13.py to look like this:

from sys import argv
string_a, string_b, string_c, string_d = argv

print "The name of your file is held in string_a:", string_a
print "The next word you type in is stored in string_b:", string_b
print "The word after that is stored in string_c:", string_c
print "And the last one is stored in string_d:", string_d

Now, run the script by typing the following into the command line:

python ex_13.py peaches are delicious!

and your output should be:

The name of your file is held in string_a: ex_13.py
The next word you type in is stored in string_b: peaches
The word after that is stored in string_c:, are
And the last one is stored in string_d:, amazing!

Hope this helps!

Kevin S.
  • 11
  • 2
1

Argv is command line arguments, and it misses values (only has one, the script name). If you want to have None for missing values you can try:

from sys import argv

argv.extend([None, None, None])
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

You could also test for number of missing arguments using len(argv) and you can also provide them as suggested in Learn Python The Hard Way:

Run the program like this:

python ex13.py first 2nd 3rd
kriss
  • 23,497
  • 17
  • 97
  • 116
  • When entering that code I get this error: "Traceback (most recent call last): File "C:\Python\LPTHW\examples_LPTHW\ex13a.py", line 4, in script, first, second, third = argv ValueError: need more than 2 values to unpack" – rayne117 Dec 22 '10 at 09:17
  • oups, my fault, I typed append instead of extend, I will correct that immediately – kriss Dec 22 '10 at 09:19
  • Where do I type in "python ex13.py first 2nd 3rd" to make the program run? I have done it in the IDLE and the command prompt, both give me the same error. I am following Zed's example exactly word for word so I don't get why it's causing me so much trouble. He says I need to run it in the "Terminal." What is that? – rayne117 Dec 22 '10 at 09:40
  • "Terminal" is Mac-ese for "command prompt". On Windows, you type it at a command prompt - not a Python REPL prompt, but a cmd.exe window prompt. It really should be working for you. – Karl Knechtel Dec 22 '10 at 10:10
  • If you have some kind of IDE (text editor with ability to run programs), there is probably some menu that opens a dialog box where you can enter command line arguments. The "terminal" things is used for launching python before the interactive python prompt. If you are at the interactive python prompt, it's too late. – kriss Dec 22 '10 at 11:57
  • @kriss, I use gedit (per Zed's instructions) to type up my programs. And then I would run them in the command prompt by switching the directories to where my programs were saved. Then I realized I could use the IDLE that comes with python so I don't have to use the command prompt. Also, I am using the 2.7 IDLE, not the 3. – rayne117 Dec 22 '10 at 22:31
1

Read farther. Page 36 of Learn Python The Hard Way, Release 1.0 says

What You Should See

Run the program like this:

python ex13.py first 2nd 3rd

ARGV takes from the command line. Run it as above and put in three variables. The script will get the name of the script and then add your three arguments.

[rick@redhat ~]$ python ex1.py 1st 2nd 3rd

The script is called: ex1.py

Your first variable is: 1st

Your second variable is: 2nd

Your third variable is: 3rd

[rick@redhat ~]$

Rick
  • 11
  • 1
1

I was having this exact same problem, trying all sorts of silly stuff in the registry and what have you as well. Turned out I was just noobing up the actual command to run the script, as stated above. It's calling for values you have to put in when you go to run it. When you get to ex14.py it would give the same error, unless you use:

python ex14.py (your name)
JMax
  • 26,109
  • 12
  • 69
  • 88
Sethdood
  • 11
  • 1
1

This happened to me. Very simple fix

You are running your code out of the python software.

All you gotta do is go to all programs > accessories > command prompt

when the command prompt window opens, then you just type "python ex13.py first second third"

if, when you just type "python" by itself and hit return you get an "undefined message" you'll need to follow these http://docs.python.org/using/windows.html to reset python as an executable "path"

I had no idea what "path" meant, basically it's just directing the computer where to go when you type "python"

jcw
  • 5,132
  • 7
  • 45
  • 54
mcnroe
  • 11
  • 1
1

I found making a simple tweak to the directions above fixed the whole issue: 1. Open Command Prompt 2. Type in: set path=%path%;C:\python26 (<--I am using 2.6.5, edit accordingly) 3. Type in: python C:/Users/research/Desktop/example.py first second third

For me, the process only worked when the FULL FILE NAME, not just example.py or ex13.py, whatever- was typed in. Good luck!

Bot
  • 11,868
  • 11
  • 75
  • 131
Lindsay
  • 11
  • 1
0

Windows < Start < Run < CMD (enter)

go to the dir that contains the argv py file, mine was C:\Python33\Python Lessons\Lesson 1\argv.py

then simply type, python argv.py first 2nd 3rd

you should see the output in the command shell window

Linux, use a bash shell and for MaxOS use their terminal window.

argv is passed the argument via the command line, that's what everyone has said here and I am only echoing their sentiments.

is there a better tutorial as the author of Learning Python the Hard Way discourages python 3.x which I think is bad for business.

lol, he goes as far as to tell people NO when asked about using Python 3.x and to reply with in 10 years!

input
  • 1
0

Exercise 13 works. I took me two days to figure it out, but it does. Here's the trick 1. You need to run it from Command Prompt (Windows, I think it's called Terminal in OSX), and not python. 2. Launch Command Prompt but DO NOT run python. That means you shouldn't be seeing the "Python 2.x.x....Type 'help', 'copyright', 'credits' message. If you are, type in quit() and hit enter.

  1. You need to be INSIDE the directory where the script sits. If not, you're going to get an [Errno 2] No such file or directory error.

  2. For me, I saved myscript.py in C:\Python27\MyStuff\ folder. So in the command prompt, I had to type in the following to make sure I was in the MyStuff directory:

cd C:\Python27\Mystuff

  1. Once in the right directory, I typed the following to run the script.

python myscript.py first second third

  1. When done correctly, my line looked like this before I hit Enter to run the script:

C:\Python27\Mystuff>python myscript.py first second third

OMG it worked. I'm still in shock. I'm so happy. Good luck!

0

If you're running multiple versions of Python on a Windows machine, you should go ahead and install Python 3.3. This is because it comes with py.exe which allows you to specify a python version as a parameter:

py -2 ex13c.py B C D

For more information see Python Windows Launcher in the official documentation.

Free Monica Cellio
  • 2,270
  • 1
  • 16
  • 15
0
from sys import argv
script,first,second,chya,dog=argv
print"Script is",script
print "first ",first
print "Second",second
print "third",chya
print "Four",dog

**

and compile your code with your arguments then it will show right answer. Because it's take only two arguments "Script and user_name" to add more arguments you have to compile those arguments at compile time after compilation

**

$ python basic_input1.py first second chya dog
Script is basic_input1.py
first  first
Second second
third chya
Four dog

Follow it

Varun Chhangani
  • 1,116
  • 3
  • 13
  • 18
0

If you don't have Python as a enviromnent variable in the windows path, you can run the code like this:

  1. Open Command Prompt
  2. Type the code like this:

    "C:\Your Path to the Python.exe" D:\Your full path to the python file.py first second third

  3. Voila!

Litwos
  • 1,278
  • 4
  • 19
  • 44
0

In the windows shell, type:

python ex13.py first second third
josliber
  • 43,891
  • 12
  • 98
  • 133
0

Well I had the same problem with the ex13 and ex14 but after long searching i found out that i was typing the wrong command after suddenly seeing the output of Zed when he was executing the code.

$ python ex13.py first 2nd 3rd <===========Type this line in command prompt and will work

The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

Do the same with ex14 and you will see its going to work.

0

If you're a Window's user, the "Terminal" is also called "Windows Powershell".

Open up Windows Powershell and first change your directory using cd C:\Users\... to wherever you saved your ex13.py file (make sure you saved it as a .py and not a .ipynb, which is a IPython notebook).

Then do:

python ex13.py first 2nd 3rd
Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
Ryan Chase
  • 2,384
  • 4
  • 24
  • 33