@click.group(context_settings=dict(help_option_names=['-h', '--help']))
def plot_glm():
    pass

@plot_glm.command()
@click.argument('path_nc')
@click.argument('out_path')
@click.argument('var_name')
@click.option('--xaxis_min', default=0.0, help='')
@click.option('--xaxis_max', default=1.1, help='')
@click.option('--xaxis_step', default=0.1, help='')
@click.option('--annotate_date', help='')
@click.option('--yr', default=0, help='')
@click.option('--date', default=-1, help='')
@click.option('--xlabel', default='', help='')
@click.option('--title', default='', help='')
@click.option('--tme_name', default='time', help='')
@click.option('--show_plot', help='')
@click.option('--any_time_data', help='')
@click.option('--format', default='%.2f', help='')
@click.option('--land_bg', help='')
@click.option('--cmap', default=plt.cm.RdBu, help='')
@click.option('--grid', help='')
@click.option('--fill_mask', help='')
def plot_map_from_nc(path_nc, out_path, var_name, xaxis_min=0.0, xaxis_max=1.1, xaxis_step=0.1,
                     annotate_date=False, yr=0, date=-1, xlabel='', title='', tme_name='time', show_plot=False,
                     any_time_data=True, format='%.2f', land_bg=True, cmap=plt.cm.RdBu, grid=False, fill_mask=False)

if __name__ == '__main__':
    plot_glm()

使用python click library(python版本2.7.11,windows 10,click版本6.6)时出现此错误:
    ctx = Context(self, info_name=info_name, parent=parent, **extra)
TypeError: __init__() got an unexpected keyword argument 'any_time_data'

我能做什么来修正这个错误?

最佳答案

似乎您试图用代码中的实际参数调用plot_map_from_ncplot_glm,如下所示:

plot_map_from_nc(any_time_data=False)
plot_glm(any_time_data=False)

它将生成与您得到的相同的错误消息。
  File "testClick.py", line 39, in <module>
    plot_glm(any_time_data=False)
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 695, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "c:\winPython\python-2.7.10.amd64\lib\site-packages\click\core.py", line 618, in make_context
    ctx = Context(self, info_name=info_name, parent=parent, **extra)
TypeError: __init__() got an unexpected keyword argument 'any_time_data'

错误原因
这是因为plot_map_from_ncplot_glm在那些click装饰器之后不是正常功能。它们是可调用的对象,其签名变成
plot_map_from_nc(args=None, prog_name=None, complete_var=None, standalone_mode=True, **extra)

plot_map_from_nc的类型是click.core.Command,传递给它的每个参数都将重定向到click.core.Command.main()
解决方案
调用这些可调用对象的正确方法是
plot_map_from_nc(sys.argv[1:]) # or
plot_map_from_nc()

如果要在代码中正常使用plot_map_from_nc,请使用其他名称定义它:
def __plot_map_from_nc__(... , any_time_data=True, ...):
    do_your_job_here
# create an alias
plot_map_from_nc = __plot_map_from_nc__
# pass this alias to click
@plot_glm.command()
@click.argument('path_nc') # ...
@click.option('--xaxis_min', default=0.0, help='') # ...
plot_map_from_nc

# Now  plot_map_from_nc becomes a   'click.core.Command'   object  while
# __plot_map_from_nc__ is still a normal function which can be invoke as
__plot_map_from_nc__(... , any_time_data=True, ...)

关于python - python click中的意外关键字参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37041126/

10-15 19:25