【发布时间】:2015-06-27 03:24:04
【问题描述】:
我正在绑定一个脚本,它将 argparse 参数传递给另一个 python
第一个脚本:t.py
import argparse
import subprocess
import os
commandLineArgumentParser = argparse.ArgumentParser()
commandLineArgumentParser.add_argument("-fname", "--fname", help="first name")
commandLineArgumentParser.add_argument("-lname","--lname", help="last name")
commandLineArguments = commandLineArgumentParser.parse_args()
fname = commandLineArguments.fname
lname = commandLineArguments.lname
print "%s\n%s" %(fname,lname)
os.system("python test1.py")
test1.py 的代码如下
import argparse
import os
print "test abc"
我想将 lname 和 fname 值传递给 test1.py 。这是他们的任何方式。
如果我在上面的代码中运行
python t.py -fname ms lname = dhoni
那么输出就是
ms
dhoni
test abc
但我希望输出如下所示
ms
dhoni
ms
dhoni
【问题讨论】:
-
您有什么理由不在 test1.py 中导入 t.py 吗?这样你就可以访问 argparse 值
-
我不知道如何在 test1.py 中导入 t.py 。是他们要导入的任何东西。如果我可以直接在 test1.py 中导入 t.py 对我很有帮助.
-
我想做一个像下面这样的命令 ..
python test1.py --lname=ms -fname=dhoni参数将直接来自 argparse.is 可以吗?这将解决我的问题。我将再次使用 argparse 接收这些值。 -
如果你想做
python test1.py --lname=ms -fname=dhoni,那么你可以按照Arthur Vaïsse 说的做。如果您想尝试导入方式,请在下面查看我的答案 -
这真的不是
argparse问题,而是how do you issue a system call that includes arguments?。argparse在t1中的使用独立于它在test1中的使用(或不使用)。