Alex
Alex

Reputation: 44315

How to setup a python code with click with commands with setup.py?

I want to be able to install some python code, using python-click and commands, with setup.py. I have the following complete code:

import click

@click.group()
@click.option(
    "-v",
    "--verbose",
    count=True,
    default=0,
    help="-v for DEBUG",
)
def cli(verbose):
    print(verbose)

@cli.command("list")
@click.option(
    "--option1"
)
def my_list_command(option1):
    print(option1)


@cli.command("find")
@click.option(
    "--option2"
)
def my_find_command(option2):
     print(option2)


if __name__ == '__main__':
   cli()

which defines two commands list and find. When I save that file as mycode.py I can do for example

python mycode.py list --option1 opt1
python mycode.py find --option2 opt2

and the code works as expected, i.e. I have two commands with their options.

But when I now try to install this code with setup.py as follows

from setuptools import setup, find_packages
setup(
    name='MyCode',
    install_requires=[
        'click',
    ],
    entry_points={
        'console_scripts': [
            'mytest=mycode.cli'
        ],
    },
)

I get the error

ERROR: For req: MyCode==0.0.1. Invalid script entry point: <ExportEntry mytest = mycode.cli:None []> - A callable suffix is required. Cf https://packaging.python.org/specifications/entry-points/#use-for-scripts for more information.

How to properly setup a multi-command python-click code, so I can use it like the above examples?

Upvotes: 0

Views: 1737

Answers (2)

talhasaleem09
talhasaleem09

Reputation: 246

This is an operator issue.

When calling a module from a package, . is used. whereas,

for calling the function from the specific module, : is used.

Check out Python-Packaging documentation.

Upvotes: -2

kmaork
kmaork

Reputation: 6012

You're almost there, just change mycode.cli to mycode:cli.

When specifying entry points, the module path is separated with ., while the function to be called by the entry points comes afterward, prefixed with a :, for example package.module:function.

For more information see the docs.

Upvotes: 6

Related Questions