So I was learning python from "Learn python the hard way" in python3 (my preferance) (I also tried everything in the 2nd python) and when I came across "Parameters, Unpacking, Variables" I was stuck for good. When I type in exactly the same thing as he does:

from sys import argv

script, first, second, third = argv

print...

when I start it up it always says:

Traceback (most recent call last):
  File "C:\Users\Vladas\mystuff\test_argv.py", line 3, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack

I understand that I am noobish at this, but can someone exlain me how to fix this, the reason why is this happening and what I am doing?

How are you running the script, and what command line arguments are you invoking the script with? If you are running the script from the command line, you would have arguments as part of the invocation, like so:

 python test_argv.py arg1 arg2 arg3

OTOH, if you are using an IDE such as IDLE, Eric, PyCharm, Dr Python, etc., you should have a menu option for entering the script arguments somewhere. Just where it is and what it is called will depend on the editor in question.

Edited 2 Years Ago by Schol-R-LEA

I've alwayes preferred the standard documentation.

Anyways, post your full source code, and tell us how you're invoking the script (your using a command line, correct? if not, you probably should be. what shell are you using? cmd/powershell with windows 8? bash in linux?).

Remember that sys.argv is just an array (where argv[0] is the script name, and the rest are the arugments). So if you know how arrays work, you can use sys.argv in a simular fasion. It's nothing special, and I don't think it even requires a special lesson.

The error ValueError: need more than 1 value to unpack tells me that the array isn't big enough, meaning that you're probably not giving it enough arguments. It's best practice to make sure that the user always provides enough arugments before attempting to unpack the array (and giving the user a "usage dialog" when it fails).

Edited 2 Years Ago by Hiroshe

I am invoking the code with IDLE, I tried to run it from cmd by shift+right click and running the cmd in the file that has the .py file. And when you mean that I need more arguments I am not really sure where the hell do I put them in and what kind of arguments does it need cause I don't even know what
from sys import argv does

The unpacking operation can only be done on tuples, and the command line arguments are stored in a list/array. So do something like this:

script, first, second, third = tuple(argv[0:])

Edited 2 Years Ago by tapananand

tapananand: Actually, that isn't true. Lists can be unpacked automatically just as tuples can. For example:

a, b, c = ['1', '2', '3']

will populate the three variables correctly. The real issue at hand is passing the command-line arguments in the first place.

VKG147: I get the impression you are unfamiliar with working in the shell, which isn't uncommon for new programmers. If you have only worked in Windows or MacOS, and haven't done any programming before, you probably have never used or even seen a shell window (also called a command line or -in the Windows world - a DOS prompt) before. I recommend closing the IDLE window and trying the following just to familiarize yourself with what is actually going on here.

I am going to assume you are using Windows 7 here; if you are using Windows 8, or an older version of Windows such as XP, you may have to adjust this accordingly.

To start the Power Shell (the modern Windows equivalent of a DOS prompt), go to the Start menu and in the programs bar enter 'PowerShell'. This should bring up at least one option for running the powershell in the Programs dialog. If that doesn't work, go to Accessories and you should find it.

Once you start Powershell, you should get a window with white text on a blue background. It will have a prompt saying the directory you are in, something like,

C:\Users\VKG147\Documents>

The flashing cursor at the end of that prompt is where text entered in the shell will go; you can't move it around with the mouse, as the only input you can give it is through the keyboard. If you type 'python' or 'python.exe', it should (if your PYTHON_PATH is set correctly) come up with a Python interpreter prompt:

Python 3.3.5 (default, Aug 16 2014, 17:29:18) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 

It won't look exactly like this, but it should be similar enough. The prompt itself is the >>> line - note the cursor position there. Try a few simple one-line Python statements, like the one I showed above, and see what happens. To exit the interpreter, type in exit() and it should bring you back to the shell prompt.

If you have a Python program file already written and in the directory (folder) you are in, you can run the program from the shell by entering:

python myprogram.py

at the shell prompt. The myprogram.py part is what is called a command-line argument for the Python interpreter, and the shell passes the arguments to the Python interpreter as a string, which the interpreter can then use to decide how to run.

Here we are coming to the meat of the problem you are having: a Python program can also have it's own arguments, which are passed to it by the Python interpreter via the sys.argv list. The program can then use those arguments to do something automatically, rather than asking for additional input from the user. For a simple example, here is a program that repeats each argument it is given on a single line:

from sys import argv

for arg in argv:
    print(arg)

Which, when invoked using the shell command:

python echo.py when in the course of human events

displays

echo.py
when
in
the
course
of
human
events

Now, you may be asking yourself, why go to all this trouble? Well, because this is how the Python IDE, IDLE, actually works under the hood. It gives a simple (in the case of IDLE, a bit too simple) GUI overlay on the shell operations to make it easier to work with, but essentially, this is what IDLE is doing for you.

Unfortunately, unlike most (better) Python editors, IDLE doesn't have a dialog for entering command arguments to pass to the program. So, if you are going to work with sys.argv, you'll need to either work from the command line shell, or find a more featureful editor such as IdleX, Eric, PyPe, PyCharm, Ninja-IDE or Sublime Text, and learn how to do it with the feature set of that IDE. Fortunately, there are many to choose from; if you do a search on 'Python IDE' or 'Python editor' you should find dozens. Almost all of them support a command-argument dialog of some sort. My personal favorites are Eric and PyCharm, but you will probably fine one that suits you best without too much trouble.

Edited 2 Years Ago by Schol-R-LEA

This article has been dead for over six months. Start a new discussion instead.