使用argparse向Python脚本中的函数发送参数

使用argparse向Python脚本中的函数发送参数,python,bash,subprocess,argparse,Python,Bash,Subprocess,Argparse,我遇到了一个奇怪的情况,需要在脚本中运行Python函数,然后从我的主代码调用脚本 我想使用子流程模块,并知道如何使用它将参数传递给纯脚本,但问题是,我需要将参数传递给其中的嵌套Python函数,其中大多数是可选的,并且具有默认值 我想,arparse会帮助我做到这一点 下面是我正在尝试的一个例子: ## Some Argparse, which will hopefully help import argparse parser = argparse.ArgumentParser() ##

我遇到了一个奇怪的情况,需要在脚本中运行Python函数,然后从我的主代码调用脚本

我想使用
子流程
模块,并知道如何使用它将参数传递给纯脚本,但问题是,我需要将参数传递给其中的嵌套Python函数,其中大多数是可选的,并且具有默认值

我想,
arparse
会帮助我做到这一点

下面是我正在尝试的一个例子:

## Some Argparse, which will hopefully help
import argparse

parser = argparse.ArgumentParser()

## All arguments, with only "follow" being required
parser.add_argument('file_name', help='Name of resulting csv file')
parser.add_argument('sub_name', help='Sub-name of resulting csv file')
parser.add_argument('follow', help='Account(s) to follow', required=True)
parser.add_argument('locations', help='Locations')
parser.add_argument('languages', help='Languages')
parser.add_argument('time_limit', help='How long to keep stream open')

args = parser.parse_args()


## Actual Function
def twitter_stream_listener(file_name=None,
                            sub_name='stream_',
                            auth = api.auth,
                            filter_track=None,
                            follow=None,
                            locations=None,
                            languages=None,
                            time_limit=20):
   ... function code ...
   ... more function code ...
   ...
   ...
   ## End of script

您可以在argparse部分中指定默认值(如果您正试图实现此目标):

然后打电话:

$ ./test.py
something

$ ./test.py --argument='somethingelse'
somethingelse

$ ./test.py --arg2=123
something
123

$ ./test.py --arg2='ipsum' --argument='lorem'
lorem
ipsum

这有用吗

您可以在argparse部分指定默认值(如果您正试图实现这一点):

然后打电话:

$ ./test.py
something

$ ./test.py --argument='somethingelse'
somethingelse

$ ./test.py --arg2=123
something
123

$ ./test.py --arg2='ipsum' --argument='lorem'
lorem
ipsum

这有用吗

你可以这样做:

import argparse

## Actual Function
def twitter_stream_listener(file_name=None,
                            sub_name='stream_',
                            auth=api.auth,
                            filter_track=None,
                            follow=None,
                            locations=None,
                            languages=None,
                            time_limit=20):

    # Your content here


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    ## All arguments, with only "follow" being required
    parser.add_argument('follow', help='Account(s) to follow')
    parser.add_argument('--file_name', help='Name of resulting csv file')
    parser.add_argument('--sub_name', help='Sub-name of resulting csv file')
    parser.add_argument('--locations', help='Locations')
    parser.add_argument('--languages', help='Languages')
    parser.add_argument('--time_limit', help='How long to keep stream open')

    args = parser.parse_args()

    twitter_stream_listener(file_name=args.file_name, sub_name=args.sub_name, follow=args.follow,
                            locations=args.locations, languages=args.languages, time_limit=args.time_limit)
follow
将是唯一必需的参数,其余参数为可选参数。可选的必须在开始时提供
--
。如果需要,您可以轻松地将模块与
子流程一起使用

使用命令行的调用示例:

python -m your_module_name follow_val --file_name sth1 --locations sth2

你可以这样做:

import argparse

## Actual Function
def twitter_stream_listener(file_name=None,
                            sub_name='stream_',
                            auth=api.auth,
                            filter_track=None,
                            follow=None,
                            locations=None,
                            languages=None,
                            time_limit=20):

    # Your content here


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    ## All arguments, with only "follow" being required
    parser.add_argument('follow', help='Account(s) to follow')
    parser.add_argument('--file_name', help='Name of resulting csv file')
    parser.add_argument('--sub_name', help='Sub-name of resulting csv file')
    parser.add_argument('--locations', help='Locations')
    parser.add_argument('--languages', help='Languages')
    parser.add_argument('--time_limit', help='How long to keep stream open')

    args = parser.parse_args()

    twitter_stream_listener(file_name=args.file_name, sub_name=args.sub_name, follow=args.follow,
                            locations=args.locations, languages=args.languages, time_limit=args.time_limit)
follow
将是唯一必需的参数,其余参数为可选参数。可选的必须在开始时提供
--
。如果需要,您可以轻松地将模块与
子流程一起使用

使用命令行的调用示例:

python -m your_module_name follow_val --file_name sth1 --locations sth2

如果要向函数传递参数,则只需在执行时将参数输入函数:

import argparse
parser=argparse.ArgumentParser()
parser.add_参数(“-o”,“--output_file_name”,help=“结果csv文件的名称”)
parser.add_参数(“-s”,“--sub_name”,default=“stream”,help=“结果csv文件的子名称”)
parser.add_参数(“-f”,“--follow”,help=“要跟随的帐户”,required=True)
add_参数(“-loc”,“--locations”,默认值=None,help=“locations”)
add_参数(“-lan”,“--languages”,默认值=None,help=“languages”)
add_参数(“-t”,“--time_limit”,默认值=20,help=“保持流打开的时间”)
options=parser.parse_args()
#然后在运行函数时传入参数
twitter\u流\u侦听器(file\u name=options.output\u file\u name,
sub_name=options.sub_name,
auth=api.auth,
过滤器\轨道=无,
follow=options.follow,
位置=选项。位置,
语言=选项。语言,
时间限制=选项。时间限制)
#或者,在定义函数时将参数传递到函数中
def twitter\u stream\u listener\u和参数(file\u name=options.output\u file\u name,
sub_name=options.sub_name,
auth=api.auth,
过滤器\轨道=无,
follow=options.follow,
位置=选项。位置,
语言=选项。语言,
时间限制=选项。时间限制):
#做点什么
通过
#然后使用默认参数运行
twitter\u stream\u listener\u with_args()

如果要将参数传递给函数,则只需在执行时将参数馈送到函数中即可:

import argparse
parser=argparse.ArgumentParser()
parser.add_参数(“-o”,“--output_file_name”,help=“结果csv文件的名称”)
parser.add_参数(“-s”,“--sub_name”,default=“stream”,help=“结果csv文件的子名称”)
parser.add_参数(“-f”,“--follow”,help=“要跟随的帐户”,required=True)
add_参数(“-loc”,“--locations”,默认值=None,help=“locations”)
add_参数(“-lan”,“--languages”,默认值=None,help=“languages”)
add_参数(“-t”,“--time_limit”,默认值=20,help=“保持流打开的时间”)
options=parser.parse_args()
#然后在运行函数时传入参数
twitter\u流\u侦听器(file\u name=options.output\u file\u name,
sub_name=options.sub_name,
auth=api.auth,
过滤器\轨道=无,
follow=options.follow,
位置=选项。位置,
语言=选项。语言,
时间限制=选项。时间限制)
#或者,在定义函数时将参数传递到函数中
def twitter\u stream\u listener\u和参数(file\u name=options.output\u file\u name,
sub_name=options.sub_name,
auth=api.auth,
过滤器\轨道=无,
follow=options.follow,
位置=选项。位置,
语言=选项。语言,
时间限制=选项。时间限制):
#做点什么
通过
#然后使用默认参数运行
twitter\u stream\u listener\u with_args()

argparse
只需获取
sys.argv
中的字符串(或显式传递给
parse\u args
的其他一些列表),并为您提供对象
args
。如何使用
args
取决于您。您可以将其视为全局变量,或者将其作为附加参数传递给
twitter\u stream\u listener
,或者只在需要时将各个属性作为参数传递。您的代码看起来不错,您是否收到任何错误?是否确实需要使用
子进程
启动程序A并在其中执行函数
A.twitter\u stream\u listener()
?更简单的方法是让你的代码导入一个
并直接调用A.twitter\u stream\u listener()
。aaaakshat,我是