如何 运行 在一个 python 脚本中使用 args 的多个脚本
How to run multiple scripts in a python script with args
我还是 python 的新手,我有多个脚本必须将参数传递给 运行。它们都在命令行中设置为 运行,例如:
python script1 -c config/config_file1.json -m import
python script2 -c config/config_file1.json -cl config/config_file2.json
python script3 -c config/config_file3.json -d name
我现在必须为 运行 上面的所有脚本创建一个脚本,我什至不知道从哪里开始。我已经看到 subprocess 或 sys.argv 的建议,但我不知道这种情况的最佳方法是什么。我所需要的只是能够 运行 主脚本到 运行 所有其他 3 个脚本,并知道处理我需要传递给它们的所有参数的最佳方法。任何建议将不胜感激。
如果脚本的参数是静态的,那么:
import subprocess
# This is like running `python script1 -c config/config_file1.json -m import` and your other commands
subprocess.call(["python", "script1", "-c", "config/config_file1.json", "-m", "import"])
subprocess.call(["python", "script2", "-c", "config/config_file1.json", "-cl", "config/config_file2.json"])
subprocess.call(["python", "script3", "-c", "config/config_file3.json", "-d", "name"])
如果你每次都需要传入不同的参数,那么你可以从input()
或者argparse
库中获取(个人更喜欢argparse
):
import argparse
import subprocess
def get_args():
parser = argparse.ArgumentParser()
# The add_argument method takes this pattern: ("--flag", "-abreviated-flag", dest="variable_to_store_input", help="Help message to display for partcular argument")
parser.add_argument("--name", "-n", dest="name", help="Name to enter: --name Bill, OR -n Bill")
parser.add_argument("--age", "-a", dest="age", help="Age to enter: --age 21, OR -a 21")
# Add as many arguments as you need following this format.
args = parser.parse_args() # Collect and parse the inputed arguments
# Make sure needed arguments are there
if not args.age:
parser.error("No age specified! Use --help for more info.")
if not args.name:
parser.error("No name specified! Use --help for more info")
return args # If this line is reached, all of the arguments are filled
arguments = get_args() # Gets the arguments
name = arguments.name
age = arguments.age
# This is just like running the command: `python script1 --name <some-name> --age <some-age>`
subprocess.call(["python", "script1", "--name", name, "--age", age])
# You can also store the command:
command = ["python", "script1", "--name", name, "--age", age]
subprocess.call(command)
我还是 python 的新手,我有多个脚本必须将参数传递给 运行。它们都在命令行中设置为 运行,例如:
python script1 -c config/config_file1.json -m import
python script2 -c config/config_file1.json -cl config/config_file2.json
python script3 -c config/config_file3.json -d name
我现在必须为 运行 上面的所有脚本创建一个脚本,我什至不知道从哪里开始。我已经看到 subprocess 或 sys.argv 的建议,但我不知道这种情况的最佳方法是什么。我所需要的只是能够 运行 主脚本到 运行 所有其他 3 个脚本,并知道处理我需要传递给它们的所有参数的最佳方法。任何建议将不胜感激。
如果脚本的参数是静态的,那么:
import subprocess
# This is like running `python script1 -c config/config_file1.json -m import` and your other commands
subprocess.call(["python", "script1", "-c", "config/config_file1.json", "-m", "import"])
subprocess.call(["python", "script2", "-c", "config/config_file1.json", "-cl", "config/config_file2.json"])
subprocess.call(["python", "script3", "-c", "config/config_file3.json", "-d", "name"])
如果你每次都需要传入不同的参数,那么你可以从input()
或者argparse
库中获取(个人更喜欢argparse
):
import argparse
import subprocess
def get_args():
parser = argparse.ArgumentParser()
# The add_argument method takes this pattern: ("--flag", "-abreviated-flag", dest="variable_to_store_input", help="Help message to display for partcular argument")
parser.add_argument("--name", "-n", dest="name", help="Name to enter: --name Bill, OR -n Bill")
parser.add_argument("--age", "-a", dest="age", help="Age to enter: --age 21, OR -a 21")
# Add as many arguments as you need following this format.
args = parser.parse_args() # Collect and parse the inputed arguments
# Make sure needed arguments are there
if not args.age:
parser.error("No age specified! Use --help for more info.")
if not args.name:
parser.error("No name specified! Use --help for more info")
return args # If this line is reached, all of the arguments are filled
arguments = get_args() # Gets the arguments
name = arguments.name
age = arguments.age
# This is just like running the command: `python script1 --name <some-name> --age <some-age>`
subprocess.call(["python", "script1", "--name", name, "--age", age])
# You can also store the command:
command = ["python", "script1", "--name", name, "--age", age]
subprocess.call(command)