简体   繁体   English

TypeError:“ str”对象无法使用Python Click库调用

[英]TypeError: 'str' object is not callable using Python Click library

I'm trying to execute a Python 3 script via the command line with the Click library but it seems that it isn't working as it should. 我正在尝试通过命令行使用Click库执行Python 3脚本,但似乎它没有按预期运行。

@click.option('--criteria', default='', type=click.STRING, envvar="CRITERIA")

That is the given line which throws the TypeError: 'str' object is not callable . 这是抛出TypeError: 'str' object is not callable的给定行TypeError: 'str' object is not callable Should I be doing something else or is it a matter of syntax? 我应该做其他事情还是语法问题?

UPDATE 更新

Having changed the placement of the criteria option few places above, now I see that the error mentioned before is given in the last placed option, no matter the type. 更改了criteria选项的位置后,在上面的几个位置上,现在我看到无论类型如何,在最后一个位置选项中都给出了前面提到的错误。 This is my method signature and the places where the parameters are used. 这是我的方法签名以及使用参数的地方。

@click.command('my_command', 'Initialize my_command')
@click.option('--s1', type=click.STRING, envvar='S_1',
              help='s1')
@click.option('--s2', type=click.STRING, envvar='S_2',
              help='s2')
@click.option('--i', type=click.STRING, envvar="I")
@click.option('--c', type=click.STRING, envvar="C")
@click.option('--l', default='[]', type=click.STRING, envvar="L")
@click.option('--st', default='[]', type=click.STRING, envvar="ST")
@click.option('--s', default='[]', type=click.STRING, envvar="S")
def my_command(s1, s2, i, c, l, st, s):
    ...

Traceback 追溯

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/project_root/__main__.py", line 27, in <module>
    @click.command('my_command', 'Initialize my_command')
  File "/project_root/venv/lib/python3.6/site-packages/click/decorators.py", line 115, in decorator
    cmd = _make_command(f, name, attrs, cls)
  File "/project_root/venv/lib/python3.6/site-packages/click/decorators.py", line 89, in _make_command
    callback=f, params=params, **attrs)
TypeError: 'str' object is not callable

This is the problem: 这就是问题:

@click.command('my_command', 'Initialize my_command')

This is the signature of click.command : 这是click.command的签名:

click.command(name=None, cls=None, **attrs)

The name defaults to the function name. name默认为函数名称。 So no need to use it because you just use the function name anyway. 因此,无需使用它,因为无论如何您只是使用函数名称。 You use a string as cls , which defaults to click.Command . 您使用字符串作为cls ,默认为click.Command

So just use it like this: 所以就这样使用它:

@click.command(help='Initialize my_command')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM