13

The error is at script, first, second, third = argv. I would like to understand why I am getting the error and how to fix it.

from sys import argv

script, first, second, third = argv
print("The script is called: ", script)
print("The first variable is: ", first)
print("The second variable is: ", second)
print("The third variable is: ", third)
5
  • 3
    you just ran the script without any arguments, for your code to work you should pass at least 3 arguments like python script.py arg1 arg2 arg3
    – dnit13
    Mar 18, 2016 at 20:01
  • What's the error you're getting?
    – Brandon
    Mar 18, 2016 at 20:01
  • I'm getting ValueError: not enough values to unpack (expected 4, got 1), is it because I'm running it in pycharm rather than using cmd?
    – sed174
    Mar 18, 2016 at 20:02
  • You'll need to tell pycharm to pass in arguments / parameters, otherwise it will simply execute the script without any arguments. Mar 18, 2016 at 21:03
  • 1
    I had the same error running the examples from "Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code", When i found your question, i said, "i known that code".
    – OJVM
    Oct 26, 2017 at 15:17

4 Answers 4

5

Run it from the shell like this:

python script.py arg1 arg2 arg3
1
  • This happens when you do not put enough arguments on the command when you run it. Feb 6, 2018 at 2:32
3

argv variable contains command line arguments. In your code you expected 4 arguments, but got only 1 (first argument always script name). You could configure arguments in pycharm. Go to Run -> Edit Configurations. Then create a new python configuration. And there you could specify Script parameters field. Or you could run your script from command line as mentioned by dnit13.

0
0

You could run it like this: python script.py first, second, third

0

I think you are running the following command:

python script.py

You are writing a program which is aksing for 4 inputs and you are giving onle one. That's why you are receiving an error. You can use the below command:

python script.py Hello How Are

Not the answer you're looking for? Browse other questions tagged or ask your own question.