How to use argv with Spyder

Question:

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
Asked By: Bazman

||

Answers:

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...'
Answered By: Mr. Polywhirl

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

Answered By: Carlos Cordoba

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

diagram

Answered By: J4cK

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')
Answered By: mors
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.