如何在 Windows 命令行中使用参数运行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17544307/
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 08:28:48  来源:igfitidea点击:

How do I run Python script using arguments in windows command line

pythonwindowspython-2.7

提问by user2563817

This is my python hello.pyscript:

这是我的pythonhello.py脚本:

def hello(a,b):
    print "hello and that's your sum:"
    sum=a+b
    print sum
    import sys

if __name__ == "__main__":
    hello(sys.argv[2])

The problem is that it can't be run from the windows command line prompt, I used this command:

问题是它不能从windows命令行提示符运行,我用了这个命令:

C:\Python27>hello 1 1

But it didn't work unfortunately, may somebody please help?

但不幸的是它没有奏效,有人可以帮忙吗?

回答by thibauts

To execute your program from the command line, you have to call the python interpreter, like this :

要从命令行执行您的程序,您必须调用 python 解释器,如下所示:

C:\Python27>python hello.py 1 1

If you code resides in another directory, you will have to set the python binary path in your PATH environment variable, to be able to run it, too. You can find detailed instructions here.

如果您的代码驻留在另一个目录中,则必须在 PATH 环境变量中设置 python 二进制路径,才能运行它。您可以在此处找到详细说明。

回答by Nils Werner

Your indentation is broken. This should fix it:

你的缩进坏了。这应该解决它:

import sys

def hello(a,b):
    print 'hello and thats your sum:'
    sum=a+b
    print sum

if __name__ == "__main__":
    hello(sys.argv[1], sys.argv[2])

Obviously, if you put the if __name__statement insidethe function, it will only ever be evaluated if you run that function. The problem is: the point of said statement is to run the function in the first place.

显然,如果将if __name__语句放在函数中,则只有在运行该函数时才会对其进行评估。问题是:上述语句的重点是首先运行该函数。

回答by falsetru

  • import sysout of hello function.
  • arguments should be converted to int.
  • String literal that contain 'should be escaped or should be surrouned by ".
  • Did you invoke the program with python hello.py <some-number> <some-number>in command line?
  • import sys出你好功能。
  • 参数应该转换为int。
  • 包含的字符串文字'应该被转义或应该被包围"
  • 您是否python hello.py <some-number> <some-number>在命令行中调用了该程序?


import sys

def hello(a,b):
    print "hello and that's your sum:", a + b

if __name__ == "__main__":
    a = int(sys.argv[1])
    b = int(sys.argv[2])
    hello(a, b)

回答by ElmoVanKielmo

import sys

def hello(a, b):
    print  'hello and that\'s your sum: {0}'.format(a + b)

if __name__ == '__main__':
    hello(int(sys.argv[1]), int(sys.argv[2]))

Moreover see @thibauts answer about how to call python script.

此外,请参阅@thibauts 关于如何调用 python 脚本的回答。

回答by C. Glass

Here are all of the previous answers summarized:

以下是之前总结的所有答案:

  • modules should be imported outside of functions.
  • hello(sys.argv[2]) needs to be indented since it is inside an if statement.
  • hello has 2 arguments so you need to call 2 arguments.
  • as far as calling the function from terminal, you need to call python .py ...
  • 模块应该在函数之外导入。
  • hello(sys.argv[2]) 需要缩进,因为它在 if 语句中。
  • hello 有 2 个参数,因此您需要调用 2 个参数。
  • 至于从终端调用函数,你需要调用 python .py ...

The code should look like this:

代码应如下所示:

import sys
def hello(a, b):
    print "hello and that's your sum:"
    sum = a+b
    print sum

if __name__== "__main__":
    hello(int(sys.argv[1]), int(sys.argv[2]))

Then run the code with this command:

然后使用以下命令运行代码:

python hello.py 1 1

回答by Pranay Kumar

There are more than a couple of mistakes in the code.

代码中有很多错误。

  1. 'import sys' line should be outside the functions as the function is itself being called using arguments fetched using sys functions.
  2. If you want correct sum, you should cast the arguments (strings) into floats. Change the sum line to --> sum = float(a) + float(b).
  3. Since you have not defined any default values for any of the function arguments, it is necessary to pass both arguments while calling the function --> hello(sys.argv[2], sys.argv[2])

    import sys def hello(a,b): print ("hello and that's your sum:") sum=float(a)+float(b) print (sum)

    if __name__ == "__main__": hello(sys.argv[1], sys.argv[2])

  1. 'import sys' 行应该在函数之外,因为使用 sys 函数获取的参数调用函数本身。
  2. 如果您想要正确的总和,您应该将参数(字符串)转换为浮点数。将 sum 行更改为 --> sum = float(a) + float(b)。
  3. 由于您尚未为任何函数参数定义任何默认值,因此在调用函数时必须传递两个参数 --> hello(sys.argv[2], sys.argv[2])

    import sys def hello(a,b): print ("hello and that's your sum:") sum=float(a)+float(b) print (sum)

    if __name__ == "__main__": hello(sys.argv[1], sys.argv[2])

Also, using "C:\Python27>hello 1 1" to run the code looks fine but you have to make sure that the file is in one of the directories that Python knows about (PATH env variable). So, please use the full path to validate the code. Something like:

此外,使用 "C:\Python27>hello 1 1" 运行代码看起来不错,但您必须确保该文件位于 Python 知道的目录之一(PATH 环境变量)中。因此,请使用完整路径来验证代码。就像是:

C:\Python34>python C:\Users\pranayk\Desktop\hello.py 1 1

回答by estellezg

I found this thread looking for information about dealing with parameters; this easy guidewas so cool:

我发现这个线程正在寻找有关处理参数的信息;这个简单的指南太酷了:

import argparse

parser = argparse.ArgumentParser(description='Script so useful.')
parser.add_argument("--opt1", type=int, default=1)
parser.add_argument("--opt2")

args = parser.parse_args()

opt1_value = args.opt1
opt2_value = args.opt2

run like:

像这样运行:

python myScript.py --opt2 = 'hi'