简体   繁体   中英

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.option('--criteria', default='', type=click.STRING, envvar="CRITERIA")

That is the given line which throws the 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. 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(name=None, cls=None, **attrs)

The name defaults to the function 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 .

So just use it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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