简体   繁体   English

是否可以从Windows命令提示符运行python脚本并同时为该脚本传递参数?

[英]Is it possible to run a python script from the windows command prompt and pass an argument for that script at the same time?

I have a saved python script. 我有一个保存的python脚本。 I run this python script from the command prompt in Windows 10. 我在Windows 10的命令提示符下运行此python脚本。

This is as simple as navigating to the directory where the script is located and then typing: 这就像导航到脚本所在的目录然后输入以下命令一样简单:

python myscript.py

and the script will run fine. 并且脚本将正常运行。

However, sometimes, I want to run this script such that a variable within that script is set to one value and sometimes to another. 但是,有时,我想运行此脚本,以便将该脚本中的变量设置为一个值,有时设置为另一个值。 This variable tells the script which port to operate an API connection through (if this is relevant). 此变量告诉脚本通过哪个端口操作API连接(如果相关)。

At the moment, I go into the script each time and change the variable to the one that I want and then run the script after that. 此刻,我每次都进入脚本并将变量更改为所需的变量,然后在此之后运行脚本。 Is there a way to set the variable at the time of running the script from the command prompt in Windows 10? 在Windows 10的命令提示符下运行脚本时,是否可以设置变量?

Or are there potentially any other efficient solutions to achieve the same flexibility at the time of running? 还是在运行时是否有其他有效的解决方案可实现相同的灵活性?

Thanks 谢谢

The usual way to do this is with command-line arguments. 通常的方法是使用命令行参数。 In fact, passing a port number is, after passing a list of filenames, almost the paradigm case for command-line arguments. 实际上,在传递文件名列表之后,传递端口号几乎是命令行参数的范例。


For simple cases, you can handle this in your code with sys.argv 对于简单的情况,您可以使用sys.argv在代码中进行处理

port = int(sys.argv[1])

Or, if you want a default value: 或者,如果要使用默认值:

port = int(sys.argv[1]) if len(sys.argv) > 1 else 12345

Then, to run the program: 然后,运行程序:

python myscript.py 54321

For more complicated cases—when you have multiple flags, some with values, etc.—you usually want to use something like argparse . 对于更复杂的情况(当您有多个标志时,有些带有值等),通常需要使用argparse东西。 But you'll probably want to read up a bit on typical command-line interfaces, and maybe look at the arguments of tools you commonly, before designing your first one. 但是在设计第一个命令行界面之前,您可能需要阅读一些典型的命令行界面,并查看常用工具的参数。 Because just looking at all of the options in argparse without knowing what you want in advance can be pretty overwhelming. 因为仅查看argparse中的所有选项而没有事先知道您想要什么,那可能会令人不知所措。


Another option is to use an environment variable. 另一种选择是使用环境变量。 This is more tedious if you want to change it for each run, but if you want to set it once for an entire series of runs in a command-line session, or even set a computer-wide default, it's a lot easier. 如果要为每次运行更改它,则比较麻烦,但是如果要在命令行会话中为整个运行系列设置一次,甚至设置计算机范围的默认值,则要容易得多。

In the code, you'd look in os.environ : 在代码中,您将查找os.environ

port = int(os.environ.get('MYSCRIPT_PORT', 12345))

And then, to set a port: 然后,设置端口:

MYSCRIPT_PORT=54321
python myscript.py

You can combine the two: use a command-line argument if present, otherwise fall back to the environment variable, otherwise fall back to a default. 您可以将两者结合起来:使用命令行参数(如果存在),否则退回到环境变量,否则退回到默认值。 Or even add a config file and/or (if you only care about Windows) registry setting. 甚至添加配置文件和/或(如果只关心Windows)注册表设置。 Python itself does something like three-step fallback, as do many major servers, but it may be overkill for your simple use case. 与许多主要服务器一样,Python本身也执行类似三步回退的操作,但是对于您的简单用例而言,它可能会显得过大。

You should look at argparse. 您应该看看argparse。 Heres an example: 这里是一个例子:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-m", help='message to be sent', type=str)
args = (parser.parse_args())

print args.m

Each argument you create is saved like a dictionary so you have to call it in your code like I did with my print statement 您创建的每个参数都像字典一样保存,因此您必须像在print语句中那样在代码中调用它

"args.m" < ---- This specifies the argument passed you want to do stuff with

here was my input/output: 这是我的输入/输出:

C:\Users\Vinny\Desktop>python argtest.py -m "Hi"
Hi

C:\Users\Vinny\Desktop>

More info on argparse: https://docs.python.org/3/library/argparse.html 有关argparse的更多信息: https : //docs.python.org/3/library/argparse.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10 在 Anaconda 命令提示符中运行 Python 脚本的 Windows 快捷方式 - Windows Shortcut to Run Python Script in Anaconda Command Prompt 如何在Windows 10中从常规命令提示符处激活python anaconda并运行脚本 - How do I activate python anaconda and run script from regular command prompt in Windows 10 为什么我不能从Windows命令提示符运行python脚本? - Why can't I run a python script from the Windows Command Prompt? 如何从 docker run 命令将命令行参数传递给 python 脚本? - How to pass command line argument to python script from docker run command? 如何从控制台运行python脚本,就像从命令提示符下调用一样? - how to run python script from the console as if called from command prompt? 通过命令提示符从 C# 运行 python 脚本 - Run python script from C# via command prompt 从命令提示符传递变量到python脚本 - passing in variable to python script run from command prompt 使用python脚本在命令提示符下运行命令 - Run commands in command prompt using python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM