Python 如何在 Spyder 中使用 argv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26679272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:51:46  来源:igfitidea点击:

How to use argv with Spyder

pythonspyder

提问by Bazman

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

我在 Spyder 中运行下面的代码。我已经将它输入到一个 py 文件中,然后只需点击运行按钮。

When I try to run it I get the error:

当我尝试运行它时,出现错误:

ValueError: need more than 1 value to unpack

ValueError:需要 1 个以上的值才能解包

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?

如此处所示,您打算在运行程序之前为 argv 变量提供输入,但我不知道如何做到这一点是 spyder?

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

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

采纳答案by Mr. Polywhirl

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.

常见的学生问题

问:当我运行它时,我得到ValueError: need more than 1 value to unpack.

请记住,一项重要的技能是关注细节。如果您查看您应该看到的内容部分,您会看到我在命令行上运行带有参数的脚本。你应该完全复制我如何运行它。

Make sure you run the command:

确保您运行以下命令:

$ python ex13.py first 2nd 3rd
$ 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
>> 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...'

回答by Carlos Cordoba

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

要传递argv到 Spyder 中的脚本,您需要转到菜单项

Run > Configuration per file

Run > Configuration per file

or press the Ctrl+F6key, then look for the option called

或按Ctrl+F6键,然后查找名为的选项

Command line options

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

one two three

回答by J4cK

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

在 Spyder 中,Run > Configure定义你的 argv 值,如下图所示,运行脚本只需按F6

diagram

图表

回答by mors

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

除了Run->Configure按照其他答案中的说明进行配置外,您还可以直接从控制台使用“runfile”。

Run the following:

运行以下命令:

   runfile('ex13.py', args='first second third')