Skip to content
Myr

setuptools.rst

This file

Path

docs/setuptools.rst

81 lines · 2.0 KiB

Last changed by

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

History of this file

Take it

1.. _setuptools-integration:23Usage with Setuptools4=====================56When writing command line utilities, it's recommended to write them as7modules that are distributed with setuptools instead of using Unix8shebangs.  There are many reasons for this.910The first one is that setuptools automatically generates executable11wrappers for Windows so your command line utilities work on Windows too.1213The second reason is that setuptools scripts work with virtualenv on Unix14without the virtualenv having to be activated.  This is a very useful15concept which allows you to bundle your scripts with all requirements into16a virtualenv.1718Introduction19------------2021To bundle your script with setuptools, all you need is the script in a Python22package and a ``setup.py`` file.2324Imagine this directory structure::2526    yourpackage/27        cli.py28    setup.py2930Contents of ``cli.py``:3132.. click:example::3334    import click3536    @click.command()37    def cli():38        """Example script."""39        click.echo('Hello World!')4041Contents of ``setup.py``::4243    from setuptools import setup, find_packages4445    setup(46        name='yourpackage',47        version='0.1',48        packages=find_packages(),49        include_package_data=True,50        install_requires=[51            'Click',52        ],53        entry_points='''54            [console_scripts]55            yourscript=yourpackage.cli:cli56        ''',57    )5859The magic is in the ``entry_points`` parameter.  Below ``console_scripts``,60each line identifies one console script.  The first part before the equals61sign (``=``) is the name of the script that should be generated, the second62part is the import path followed by a colon (``:``) with the click63command.6465That's it.6667Testing The Script68------------------6970To test the script, you can make a new virtualenv and then install your71package::7273    $ virtualenv venv74    $ . venv/bin/activate75    $ pip install --editable .7677Afterwards, your command should be available:7879.. click:run::8081    invoke(cli, prog_name='yourscript', prog_prefix='')