32

I'm running the code below in Spyder. I have typed it in a py file and simply hit the run button.

When I try to run it I get the error:

ValueError: need more than 1 value to unpack

As shown here you are meant to give the inputs for the argv variable before running the program but I don't know how to do this is spyder?

http://learnpythonthehardway.org/book/ex13.html

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 "Your third variable is:", third
Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
Bazman
  • 2,058
  • 9
  • 45
  • 65
  • print out `argv`, looks like it is a scalar and not a tuple. What are your command line arguments when calling the script? Did you run `python ex13.py first 2nd 3rd`? Make sure your supply args. – Mr. Polywhirl Oct 31 '14 at 16:33

4 Answers4

81

To pass argv to a script in Spyder, you need to go the menu entry

Run > Configuration per file

or press the Ctrl+F6 key, then look for the option called

Command line options

on the dialog that appears after that, and finally enter the command line arguments you want to pass to the script, which in this case could be

one two three

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
  • @FTilmann, please take a closer look at the dates. My answer was given on Nov/2014, while Trickster answer was given just a bit more than a month ago. – Carlos Cordoba Aug 20 '15 at 14:04
  • I do apologise. I misread the day (13) as the year. Will try to be more careful next time - I deleted my comment as it was clearly inappropriate and based on a stupid mistake of mine. – F Tilmann Aug 21 '15 at 14:26
  • @CarlosCordoba I know this thread is old but is there a faster way to do this now or does one still have to go through Ctrl+F6? Thanks – trustory Feb 24 '20 at 01:41
  • 1
    This has not changed in Spyder 4, sorry. – Carlos Cordoba Feb 24 '20 at 15:10
42

In addition to configuring in the Run->Configure as explained in other answers, you can use "runfile" directly from the console.

Run the following:

   runfile('ex13.py', args='first second third')
mors
  • 641
  • 6
  • 6
21

In Spyder, go Run > Configure and define your argv values as showing in following diagram and to run the script just press F6

diagram

J4cK
  • 30,459
  • 8
  • 42
  • 54
-16

Read the FAQ at the bottom of the page, it specifically mentions this error.

Common Student Questions

Q. When I run it I get ValueError: need more than 1 value to unpack.

Remember that an important skill is paying attention to details. If you look at the What You Should See section you see that I run the script with parameters on the command line. You should replicate how I ran it exactly.

Make sure you run the command:

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

You can ensure that the arguments are supplied.

if __name__ == '__main__':
    if len(argv) == 4:
        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
    else:
        print 'You forgot the args...'
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • But where do I enter this in console? That doesn't seem to work either? I enter in console: $ python ex13.py first 2nd 3rd and I get the error: $ python ex13.py first 2nd 3rd File "", line 1 $ python ex13.py first 2nd 3rd ^ SyntaxError: invalid syntax – Bazman Oct 31 '14 at 16:44
  • To be clear should I add $ python ex13.py first 2nd 3rd to the scipt or run it in console prior to running the script. Right now neither way seems to work. – Bazman Oct 31 '14 at 16:46
  • You call the script in the shell/terminal/bash. This example deals with command-line arguments, not the Idle. Open a terminal and run "`python ex13.py first 2nd 3rd`" – Mr. Polywhirl Oct 31 '14 at 16:47
  • OK I hope this isn't too stupid a question but how do I do that? – Bazman Oct 31 '14 at 16:48
  • 2
    Using [Mac](https://www.youtube.com/watch?v=eYK7NY0BsaA) and [Windows](https://www.youtube.com/watch?v=6x2qtLrQdqU) but this is the most helpful in your situation, watch this: [Youtube: Command Line Arguments in Python](https://www.youtube.com/watch?v=d3uv23jvp4w) – Mr. Polywhirl Oct 31 '14 at 17:06
  • 11
    I think you are confusing @Bazman He asked about how to pass `argv` to a script ran inside Spyder. That's different from running it in a regular terminal. I'll add a proper answer below. – Carlos Cordoba Nov 05 '14 at 20:15
  • 1
    @Mr.Polywhirl has answered a different question here. The OP is specifically asking how to use command line arguments from within the Spyder IDE. See Carlos' answer below. – feedMe Feb 14 '17 at 13:07
  • 2
    This answer is completely different from what is being asked! – Dr. Essen May 23 '18 at 03:52
  • this is not an answer to the question – KansaiRobot Apr 08 '20 at 13:33