How to use argv with Spyder

别等时光非礼了梦想. 提交于 2019-12-17 06:50:55

问题


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

回答1:


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

Common Student Questions

Q. When I run it I getValueError: 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...'



回答2:


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




回答3:


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




回答4:


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')


来源:https://stackoverflow.com/questions/26679272/how-to-use-argv-with-spyder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!