Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

79 lines · 2.0 KiB

Last changed by

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

History of this file

Take it

1Usage with Setuptools2=====================34When writing command line utilities it's recommended to write them as5modules that are distributed with setuptools instead of using UNIX6shebangs.  There are a bunch of reasons for this.78The first one is that setuptools automatically generates executable9wrappers for Windows so your command line utilities work on Windows too.1011The second reason is that setuptools scripts work with virtualenv on UNIX12without the virtualenv having to be activated.  This is a very useful13concept which allows you to bundle your scripts with all requirements into14a virtualenv.1516Introduction17------------1819All you need to do to bundle your scripts with setuptools is your script20in a python package and a ``setup.py`` file.2122Imagine this layout::2324    yourpackage/25        cli.py26    setup.py2728Contents of ``cli.py``:2930.. click:example::3132    import click3334    @click.command()35    def cli():36        """Example script."""37        click.echo('Hello World!')3839Contents of ``setup.py``::4041    from setuptools import setup, find_packages4243    setup(44        name='yourpackage',45        version='0.1',46        packages=find_packages(),47        include_package_data=True,48        install_requires=[49            'Click',50        ],51        entry_points='''52            [console_scripts]53            yourscript=yourpackage.cli:cli54        ''',55    )5657The magic is in the ``entry_points`` parameter.  Below ``console_scripts``58each line identifies one console script.  The first part before the equals59sign (``=``) is the name of the script that should be generated, the second60part is the import path followed by a colon (``:``) with the click61command.6263That's it.6465Testing The Script66------------------6768To test the script you can make a new virtualenv and then install your69package::7071    $ virtualenv venv72    $ . venv/bin/activate73    $ pip install --editable .7475Afterwards your command should be available:7677.. click:run::7879    invoke(cli, prog_name='yourscript', prog_prefix='')