-1

I was reading LEARN PYTHON THE HARD WAY by Zed A. Shaw. In chapter 13 I got the following code

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)

I tried to run this on spyder but the error occurs.I gonna paste the last lines of spyder console ValueError: not enough values to unpack (expected 4, got 1)

Please help me to figure out the issue, thanks.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
moniker
  • 23
  • 1
  • 8
  • 1
    1. If you get errors, you need to post them here. Saying that you get errors, then not saying what they are isn't very helpful. 2. How are you running the script? Are you passing any arguments? – Carcigenicate Sep 10 '17 at 13:04
  • Please have a look at this [argv](https://stackoverflow.com/questions/13263951/what-is-argv-and-what-does-it-do) – ABcDexter Sep 10 '17 at 13:13
  • "I was reading LEARN PYTHON THE HARD WAY by Zed A. Shaw. In chapter 13 " The necessary explanation is **in the surrounding text**. I happen to think that LPTHW is a terrible textbook, but it at least does a reasonable job of explaining what it's talking about, at the times it talks about them. Where it doesn't, it's because the author is expecting the reader to **use a search engine** with relatively obvious terms in order to understand the concept. – Karl Knechtel Aug 09 '22 at 08:16

4 Answers4

4

You aren't passing in any command line arguments when you run the script, even though it's expecting 3 arguments. You're getting an error because you're trying to extract more values out of args than it contains.

Command line arguments are data that you're giving to the program. Just pass some dummy data as arguments when you run your script:

python your_script_here.py 1 2 3

The 1 2 3 are the arguments being passed.

My example was modified from this tutorial. It's worth a read-over.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1
# your_code.py
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)

You can run it this way :

python your_code.py one two three

So you get :

The script is called: your_code.py
Your first variable is: one
Your second variable is: two
Your third variable is : three

Regards

gretiere
  • 11
  • 2
0

The error you are getting is because you are not passing any arguments when running the file. Trying passing arguments while running the file. An example

python foo.py 1, 2, 3  
0

In python, sys.argv is a list which contains the command-line arguments passed to the script.

So for instance you could create a python file named testingArguments.py which would print the arguments parsed from the command-line.

The code for this may simply look something like:

from sys import argv
print(argv)

then from the command line, if you ran the command:

python testingArguments.py arg1 arg2 3 

then it would print out the arguments in a list so:

['testingArguments.py', 'arg1', 'arg2', '3']

what your code does, is get this list in the same way, then unpacks it just like you can a list not from sys.argv:

a, b, c, d = [1, 2, 3, 4]

now a=1, b=2, c=3 and d=4.

so hopefully you can see now that altogether, your code just prints out the four arguments passed at the command line which are the script name and 3 more arguments as above.

The error that you are getting:

ValueError: not enough values to unpack (expected 4, got 1)

is because you are not passing in these 3 extra variable so sys.argv can't be unpacked into 4 as it only has one element: the script name. Hence the last bit: (expected 4, got 1).

Hope this helps!

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54