【问题标题】:How to pass argparse arguments to another python program?如何将 argparse 参数传递给另一个 python 程序?
【发布时间】: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"

我想将 lnamefname 值传递给 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?argparset1 中的使用独立于它在test1 中的使用(或不使用)。

标签: python argparse


【解决方案1】:

试试这个test1.py

from sys import argv
print "%s\n%s" % (argv[1], argv[2])

【讨论】:

    【解决方案2】:

    嗯,我不明白你为什么要这么做,但你真的已经拥有完成这项任务所需的一切:

    第一个python脚本(我称之为sof.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))
    command = "python sof2.py {arg1} {arg2}".format(arg1=fname, arg2=lname)
    os.system(command)
    

    第二个python脚本(这里是sof2.py)

    import argparse
    import subprocess
    import os
    
    commandLineArgumentParser = argparse.ArgumentParser()
    commandLineArgumentParser.add_argument("fname")
    commandLineArgumentParser.add_argument("lname")
    commandLineArguments = commandLineArgumentParser.parse_args()
    
    fname = commandLineArguments.fname
    lname = commandLineArguments.lname
    
    print "%s\n%s" %(fname,lname)
    

    这给了我以下执行:

    python3 sof.py -fname foo -lname bar
    foo
    bar
    foo
    bar
    

    注意:我使用 python3,但如果您必须使用 python2,此代码仍然正确,只需删除 print 周围的 ()

    【讨论】:

    • 非常感谢您的回复。如果我稍微更改了命令行而不是输出。更改意味着如果我尝试了 python sof.py -lname bar -fname foo
    • 这行不通。我在第二个脚本中将fnamelname 定义为位置参数。如果 argparse 参数以 - 字符开头,它将成为可选参数并且必须按此输入。因此,在调用脚本时,参数-foo-bar 是可选的,因此您必须编写script -bar barvalue -foo foovalue,但是如果您在开始时定义的参数没有-,那么它就会成为位置参数。因此,foobar 调用变为:script foovalue barvalue。您不能使用 - 调用 fnamelname,您还必须将其添加到 argparse 参数中。
    【解决方案3】:

    您可以修改 test1.py 以包含 t.py 并直接访问 argparse 变量。

    test1.py

    import t.py
    print t.fname, t.lname
    

    【讨论】:

    • 你的回答最有道理,除非os.system("python test1.py") 运行位是必不可少的。
    【解决方案4】:

    将你的 test1.py 更改为:

    import argparse
    import os
    import sys
    
    print sys.argv
    print "test abc"
    

    并从t.py 致电您的test1.py 为:

    os.system("python test1.py vv gg hh")
    

    现在您的参数vv gg hh 可以在test1.py 中使用。

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2013-11-30
      • 2018-03-07
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多